Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed replace versus python replace

Tags:

python

sed

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 | 
like image 338
user1440188 Avatar asked Dec 08 '25 13:12

user1440188


2 Answers

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/'
like image 65
Michael Mrozek Avatar answered Dec 10 '25 02:12

Michael Mrozek


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).

like image 42
mgilson Avatar answered Dec 10 '25 03:12

mgilson