bash sed:替换行尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18271110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sed: replacing end of line
提问by loentar
I need to quote end of line and using thissolution to replace end of line which working perfectly when file has multiple lines.
我需要引用行尾并使用此解决方案来替换行尾,当文件有多行时,它可以完美地工作。
Problem arises when I try to use this method with files that have no \nor have single line ending with \n. In this case seddoes not replace anything, even second s/command does not work. Also looks like last end of line is never replaced.
当我尝试将此方法用于没有\n或单行以\n. 在这种情况下sed不会替换任何东西,即使第二个s/命令也不起作用。看起来也永远不会替换行尾。
My command is:
我的命令是:
sed ':a;N;$!ba;s/\n/\n/g;s/a/_/g' <file>
(replace \nto \\nand replace ato _for example).
(替换\n到\\n和替换a到_例如)。
I use hexdump -Cto display files here:
我用来hexdump -C在这里显示文件:
There are tests I made:
我做了一些测试:
$ # Test1. single line without \n
$ echo -n 'abc' | sed ':a;N;$!ba;s/\n/\n/g;s/a/_/g' | hexdump -C
00000000 61 62 63 |abc|
# but expected "_bc"
$ # Test2. single line with \n
$ echo 'abc' | sed ':a;N;$!ba;s/\n/\n/g;s/a/_/g' | hexdump -C
00000000 61 62 63 0a |abc.|
# but expected "_bc\n"
$ # Test3. two lines
$ echo -e 'abc\n' | sed ':a;N;$!ba;s/\n/\n/g;s/a/_/g' | hexdump -C
00000000 5f 62 63 5c 6e 0a |_bc\n.|
# but expected "_bc\n\n"
Questions:Why second s/does not replace anything in Test1 and Test2? Is there any method to fix replacing of s/\n/\\n/and s/a/_/gin all this tests?
问题:为什么 seconds/不替换 Test1 和 Test2 中的任何内容?是否有任何方法可以修复所有这些测试中的替换s/\n/\\n/和替换s/a/_/g?
P.S.: I don't want some workaround like adding newline at end of stream before sed processing and removing it after.
PS:我不想要一些解决方法,例如在 sed 处理之前在流末尾添加换行符并在之后将其删除。
EDIT:looks like Ncommand does not read single line, even if it followed by '\n'. Any idea how to fix it?
编辑:看起来N命令不会读取单行,即使后面跟着“\n”。知道如何修复它吗?
EDIT2:Seems like this is expected behavior of Ncommand. Is there any method on how to replace last line ending?
采纳答案by loentar
Sad, but I don't find any method to prevent sed from adding new line to output when Ncommand is used. I didn't want to some workaround to like add new line before processing and removing it after, but seems like there is no other method.
可悲,但我没有找到任何方法来防止 sed 在使用N命令时向输出添加新行。我不想在处理之前添加新行并在之后删除它,但似乎没有其他方法。
This is the final solution with workaround:
这是带有变通方法的最终解决方案:
sed ':a;$!{N;ba};s/\n/\n/g;s/a/_/g' <<< "$VAR" | tr -d '\n'
<<<like echo adds new line into input string, trremoves it.
<<<就像 echo 在输入字符串中添加新行,tr将其删除。
With that line all test are OK:
使用该行所有测试都可以:
$ echo -e "abc" | sed ':a;$!{N;ba};s/\n/\n/g;s/a/_/g' | tr -d '\n' | hexdump -C
00000000 5f 62 63 |_bc|
$ echo -e "abc\n" | sed ':a;$!{N;ba};s/\n/\n/g;s/a/_/g' | tr -d '\n' | hexdump -C
00000000 5f 62 63 5c 6e |_bc\n|
$ echo -e "abc\n\n" | sed ':a;$!{N;ba};s/\n/\n/g;s/a/_/g' | tr -d '\n' | hexdump -C
00000000 5f 62 63 5c 6e 5c 6e |_bc\n\n|
P.S.: thanks potong for figuring out the :a;$!{N;ba}.
PS:感谢potong找出:a;$!{N;ba}.
P.P.S.: If someone will give working answer without tr, I will re-accept it.
PPS:如果有人会在没有 的情况下给出有效答案tr,我会重新接受。
回答by potong
This might work for you (GNU sed):
这可能对你有用(GNU sed):
sed ':a;$!{N;ba};s/\n/\n/g;s/a/_/g' file
or:
或者:
sed ':a;$!N;$!ba;s/\n/\n/g;y/a/_/' file
Logically it makes sense: one cannot get the nextline if you are on the lastline.
从逻辑上讲,这是有道理的:next如果您在线,则无法获得last线路。
回答by devnull
Can you use perlinstead?
可以perl代替吗?
$ echo -n "abc" | perl -pe 's/\n$/\n/;' -pe 's/a/_a/;' | od -x
0000000 615f 6362
0000004
$ echo "abc" | perl -pe 's/\n$/\n/;' -pe 's/a/_a/;' | od -x
0000000 615f 6362 6e5c
0000006
$ echo -e 'abc\n' | perl -pe 's/\n$/\n/;' -pe 's/a/_a/;' | od -x
0000000 615f 6362 6e5c 6e5c
0000010

