Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
1  
works for me... –  M'vy Jul 6 '11 at 9:57

2 Answers 2

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>
share|improve this answer
    
Would that actually have any impact on the result? –  Michael Mrozek Jul 6 '11 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 Jul 6 '11 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 Jul 6 '11 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 Jul 7 '11 at 7:04

MS-line-ends in job.csv?

 less job.csv

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

share|improve this answer
    
I'll check tomorrow morning when back in the office. –  mhitza Jul 6 '11 at 17:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.