linux+sed+替换+回车_sed 替换换行回车

A carriage return linefeed (CRLF) is a special sequence of characters, used by DOS/Windows, to signify the end of a line of text. However, in *nix, only a linefeed (LF) is required to signify a line ending.

test.CRLF.txt

12345CRLF

12345LF

Delete/Replace a Carriage Return (CR) with sed

There are a few ways to remove or replace a carriage return with sed.

sed 's/.$//' test.CRLF.txt > test1.txt

12345LF

1234LF

sed 's/\x0D$//'test.CRLF.txt > test2.txt

12345LF

12345LF

sed 's/\r//'test.CRLF.txt > test3.txt

12345LF

12345LF

The first method assumes all lines end with CRLF, while the second and third method depend on the version of sed.

Delete/Replace a Linefeed (LF) with sed

By default, sed strips the newline as the line is placed into the pattern space. However, the workaround is to read the whole file into a loop first.

sed ':a;N;$!ba;s/\n//g'

where

:a       - create a label 'a'

N        - append the next line to the pattern space

$!       - if not the last line

ba       - branch (go to) label 'a'

s        - substitute

/\n/     - regex for new line

// - with text ""

g        - global match (as many times as it can)

Example: Delete Newlines (CRLF) with sed

Delete the carriage return first, and then delete the linefeed.

sed -i 's/\r//g' example.txt

sed example delete newline carriage return.png

sed -i ':a;N;$!ba;s/\n//g' example.txt

sed example delete newline linefeed.png

tr: A Simpler Way to Replace or Delete a Newline (CRLF)

The tr utility is a preferred and simpler method for replacing end of line characters as the pattern buffer is not limited like in sed.

tr '\r\n' '' < input_file > output_file

or to delete the newline (CRLF),

tr -d '\r\n' < input_file > output_file

where

\r - carriage return

\n - linefeed

Example: Delete Newlines (CRLF) with tr

tr -d '\r\n' < example.txt > example2.txt

tr example delete newline CRLF.png

Command Line Examples

Note: The command cat will concatenate files and print on the standard output. The option A will use ^ and M notation to show non-printing characters, display $ at end of each line, and display TAB characters as ^I.

REF:

http://www.canbike.org/information-technology/sed-delete-carriage-returns-and-linefeeds-crlf.html

ABHD12

ABI2

ACSM2A

ACSM2B

AGPAT5

ANP32B

APP

ARID1A

ARL10

BACH2

FBXL2

FBXO22

FBXO30

FBXO39

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用sed命令将空格替换回车,具体命令如下: ``` sed 's/ /\n/g' filename ``` 其中,s表示替换操作,/ /表示要替换的空格,\n表示回车,g表示全局替换,filename表示要操作的文件名。 上述命令的含义是将filename文件中的所有空格都替换回车,并输出结果到标准输出。如果需要将结果保存到文件中,可以使用重定向操作符(>)。例如: ``` sed 's/ /\n/g' filename > newfile ``` 这个命令的含义是将filename文件中的所有空格都替换回车,并将结果保存到newfile文件中。 ### 回答2: 在Linux中,sed命令是一种用于对文本进行流编辑的强大工具。要将空格替换回车,可以使用sed命令的s命令来完成。 要在一行文本中将空格替换回车,可以使用以下命令: sed 's/ /\ /g' 其中,s表示进行替换操作,/ /表示要替换的模式,\表示回车符,g表示全局替换,即替换每个匹配的空格。 对于多行文本,可以将文件作为输入,并使用同样的命令: sed 's/ /\ /g' filename.txt 其中,filename.txt是要进行操作的文件名。 此命令将把每个空格替换回车,并将修改后的文本输出到终端。如果要将结果保存到文件中,可以使用重定向符号将输出重定向到一个新文件中: sed 's/ /\ /g' filename.txt > newfile.txt 这样,新的文件newfile.txt将包含替换后的文本。 sed命令是一个非常强大的编辑工具,它支持很多其他的操作和选项,可以根据具体需求进行更复杂的文本处理。 ### 回答3: 在Linux中,使用sed命令将空格替换回车是一个相对简单的操作。可以使用以下命令实现: ```shell sed 's/ /\n/g' filename ``` 其中,`filename`是要处理的文件名称。 这个命令中的`s/ /\n/g`表示将其中的一个空格替换为一个回车符,并且通过`g`选项实现全局替换,即将文件中的所有空格都替换回车符。 这样,当命令执行后,空格字符就会被替换回车符,从而实现了所需的操作。 需要注意的是,如果要直接修改源文件,可以使用`-i`选项: ```shell sed -i 's/ /\n/g' filename ``` 这样命令会直接修改`filename`文件中的内容,而不是在标准输出中显示结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值