unix sed command for escaped chars
I try to replace the char # with a newline \n:
sed -e "s/#/\n/g" > file.txt
this works fine.
Now there is problem with the char #, when is escaped with -:
eg.: 123#456-#789#777 should be:
123 456#789 777The escaped char - itself can also be escaped:
eg.: 123#456-#789--012#777 should be:
How ca开发者_Go百科n i do this with sed ?
$ echo "123#456-#789#777" | sed 's/\([^-]\)#/\1\n/g;s/-\(.\)/\1/g'
123
456#789
777
$ echo "123#456-#789--012#777" | sed 's/\([^-]\)#/\1\n/g;s/-\(.\)/\1/g'
123
456#789-012
777
$ echo "#123#456-#789#777" | sed 's/\(^\|[^-]\)#/\1\n/g;s/-\(.\)/\1/g'
123
456#789
777
$ echo "#1###2-#3--4#5#" | sed "s/\([^-]\)##/\1#\n/g;s/\(^\|[^-]\)#/\1\n/g;s/-\(.\)/\1/g"
1
2#3-4
5
$
First part is to preserve -<any chars> and convert # (without the preceding -) to newline, second part is to convert -<any chars> to only <any chars> (If you don't need such wildcard, I think you know how to modify for only - and #.)
sed -e "s/([^-])#/\1\n/g;s/--/-/g;s/-#/#/g"
This only escapes -- and -# though.
$ echo "123#456-#789--012#777" | awk -F"#" '{print $1;for(i=2;i<NF-1;i++){printf "%s#",$i};print $(NF-1);print $NF}'
123
456-#789--012
777
$ echo "123#456-#789#777" | awk -F"#" '{print $1;for(i=2;i<NF-1;i++){printf "%s#",$i};print $(NF-1);print $NF}'
123
456-#789
777
加载中,请稍侯......
精彩评论