简体   繁体   中英

sed replace versus python replace

The format of the output is the same except that when using python, the output adds a new line between each string. Any idea why and how I can do this using sed?

sed 's:\x01: | :g'

01.06.2012 07:51:55.135 | 3732 | INFO | xxx | 8=FIX.4.2 | 9=157 | 35=V | 56=xxx | 49=xxx | 52=20120601-07:51:54 | 34=40 | 262=VMD1338537114945 | 263=1 | 264=0 | 265=1 | 
01.06.2012 07:51:55.135 | 3732 | INFO | xxx | 8=FIX.4.2 | 9=157 | 35=V | 56=xxx | 49=xxx | 52=20120601-07:51:54 | 34=41 | 262=VLT1338537114945 | 263=1 | 264=1 | 265=1 | 

for line in sys.stdin:
    print line.replace("\01", " | ")


01.06.2012 07:51:55.135 | 3732 | INFO | xxx | 8=FIX.4.2 | 9=157 | 35=V | 56=xxx | 49=xxx | 52=20120601-07:51:54 | 34=40 | 262=VMD1338537114945 | 263=1 | 264=0 | 265=1 | 

01.06.2012 07:51:55.135 | 3732 | INFO | xxx | 8=FIX.4.2 | 9=157 | 35=V | 56=xxx | 49=xxx | 52=20120601-07:51:54 | 34=41 | 262=VLT1338537114945 | 263=1 | 264=1 | 265=1 | 

I'm not a sed guru, but you can add a newline at the end of every line in sed using the following:

 sed -e 's/$/\n/g'

This works because $ matches the end of a line. Note that you can string multiple sed commands together with -e (as pointed out by Michael Mrozek -- see man sed for more info).

Add a second substitution that replaces the end of the line with a newline: s/$/\\n/ . Since you have two patterns now, you need to use -e :

sed -e 's:\x01: | :g' -e 's/$/\n/'

This might work for you:

sed '/\x01/{s// | /g;G}' file

Or if you just want to add a newline to every line:

sed G file

print添加一个新行试试这个

sys.stdout.write(line.replace("\01"," | "))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM