Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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'                                                                                                   

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

~> cat job.csv | sed 's/"//g;s/^/<id>/g;s/$/<\/id>/g' | head                                                                                         

~> head job.csv                                                                                                                                      

What do you guys think can be the issue?

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

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

You might be better off without the useless cat:

$ cat test.csv 
$ sed 's/"//g;s/^/<id>/g;s/$/<\/id>/g' test.csv 
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
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

Your Answer


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.