我有一些线,LF结束和大部分线路与CR + LF结束了1.5 GB的Windows文本文件
可以请你sed
脚本帮助其
我试图做的文本编辑器的所有替换,但花了很长的文件中执行所有的替代品(1%,半小时)。我试着fart
来替代它:
fart -c -B -b text.txt "\r\n" "$|$"
用下面的结果
replacement 0 occurence(s) in 0 file(s)..
一个使用awk:
$ awk '{sub(/(^|[^\r])$/,"&\r")}1' file
测试它(0X0A是LF,0X0D是CR):
$ awk 'BEGIN{print "no\nyes\r\n\n\r"}' > foo
$ hexdump -C foo
00000000 6e 6f 0a 79 65 73 0d 0a 0a 0d 0a |no.yes.....|
0000000b
$ awk '{sub(/(^|[^\r])$/,"&\r")}1' foo > bar
$ hexdump -C bar
00000000 6e 6f 0d 0a 79 65 73 0d 0a 0d 0a 0d 0a |no..yes......|
0000000d
我这样做:首先在该行的末尾删除所有\r
,然后\r
明确添加到该行的末尾。
sed -e 's/\r$//' -e 's/$/\r/' file
这里有一个演示:
$ printf "1\r\n2\n3\n4\r\n5\n" > file
$ od -c file
0000000 1 \r \n 2 \n 3 \n 4 \r \n 5 \n
0000014
$ sed -i -e 's/\r$//' -e 's/$/\r/' file
$ od -c file
0000000 1 \r \n 2 \r \n 3 \r \n 4 \r \n 5 \r \n
0000017
这是GNU sed的。
所以,很简单只是安装一个util like unix2dos
自动做的。与转换CR + LF为$的unix2dos
所提出的中间步骤| $,(和背面),是没有必要的。演示:
# first dump a file with both *DOS* and *Unix* style line endings:
hexdump -C <({ seq 2 | unix2dos ; seq 3 4; } )
# the same file, run through unix2dos
hexdump -C <({ seq 2 | unix2dos ; seq 3 4; } | unix2dos)
输出:
00000000 31 0d 0a 32 0d 0a 33 0a 34 0a |1..2..3.4.|
0000000a
00000000 31 0d 0a 32 0d 0a 33 0d 0a 34 0d 0a |1..2..3..4..|
0000000c
或者更精心,一前/表之后,(见man hexdump
有关格式化的详细说明):
hdf() { hexdump -v -e '/1 "%_ad# "' -e '/1 " _%_u\_\n"' $@ ; }
# Note: the `printf` stuff keeps `paste` from misaligning the output.
paste <(hdf <({ seq 2 | unix2dos ; seq 3 4; }) ; printf '\t\n\t\n' ; ) \
<(hdf <({ seq 2 | unix2dos ; seq 3 4; } | unix2dos ))
输出:
0# _1_ 0# _1_
1# _cr_ 1# _cr_
2# _lf_ 2# _lf_
3# _2_ 3# _2_
4# _cr_ 4# _cr_
5# _lf_ 5# _lf_
6# _3_ 6# _3_
7# _lf_ 7# _cr_
8# _4_ 8# _lf_
9# _lf_ 9# _4_
10# _cr_
11# _lf_