2

Simply put I have a small sed command that is supposed to trim " and wrap the input between the <id> tag. It goes well single line and multiple line input, but badly misbehaves on an input file (via cat or as a sed parameter)

Sample outputs (and as the final command a head on the file).

~> echo 200 | sed 's/"//g;s/^/<id>/g;s/$/<\/id>/g'                                                                                                   
<id>200</id>

~> sed 's/"//g;s/^/<id>/g;s/$/<\/id>/g' <<EO                                                                                                         
heredoc> 2301930
heredoc> 1230910293
heredoc> 102391093
heredoc> 
heredoc> EO
<id>2301930</id>
<id>1230910293</id>
<id>102391093</id>
<id></id>

~> cat job.csv | sed 's/"//g;s/^/<id>/g;s/$/<\/id>/g' | head                                                                                         
</id>79116
</id>79125
</id>79126
</id>78327
</id>78284
</id>78288
</id>78291
</id>78304
</id>78311
</id>78335

~> head job.csv                                                                                                                                      
"179116"
"179125"
"179126"
"178327"
"178284"
"178288"
"178291"
"178304"
"178311"
"178335"

What do you guys think can be the issue?

1
  • 1
    works for me...
    – M'vy
    Commented Jul 6, 2011 at 9:57

2 Answers 2

2

MS-line-ends in job.csv?

 less job.csv

often shows ^M at the end of line, if it is.

1
  • I'll check tomorrow morning when back in the office.
    – mhitza
    Commented Jul 6, 2011 at 17:06
0

You might be better off without the useless cat:

$ cat test.csv 
"179116"
"179125"
"179126"
"178327"
"178284"
"178288"
"178291"
"178304"
"178311"
"178335"
$ sed 's/"//g;s/^/<id>/g;s/$/<\/id>/g' test.csv 
<id>179116</id>
<id>179125</id>
<id>179126</id>
<id>178327</id>
<id>178284</id>
<id>178288</id>
<id>178291</id>
<id>178304</id>
<id>178311</id>
<id>178335</id>
4
  • Would that actually have any impact on the result? Commented Jul 6, 2011 at 15:23
  • I've said it in my question, the problem persisted either way "misbehaves on an input file (via cat or as a sed parameter)".
    – mhitza
    Commented Jul 6, 2011 at 17:07
  • Oh, and I don't think cat is useless, I always start my commands with cat, do a sed, maybe replace that with a tr, then an awk, replace the tr with sed back. You get the idea, that way I don't shuffle around the input file.
    – mhitza
    Commented Jul 6, 2011 at 17:09
  • 1
    cat isn't useless, but superfluous in your command. *nix commands usually take a filename as a parameter or support processing via stdin (command < file). And sed doesn't "shuffle around" the input file unless you tell it to with -i.
    – l0b0
    Commented Jul 7, 2011 at 7:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .