Reputation: 1795
I want to edit a file from the command line, because opening it in vim or other editors takes forever (a large file). I want to add a string ('chr') to the beginning of every line that is not commented out with a #. The command I am using is this:
cat '/home/me/37.vcf' | sed s/^/chr/>'sp.vcf'
But it adds a chr to the beginning of EVERY line and a > to the END of every line. I don't want either of those things to occur. Can anyone offer any suggestions to improve my results?
Upvotes: 0
Views: 201
Reputation: 58558
This might work for you (GNU sed):
sed '/^\s*#/!s/^/chr/' file > new_file
Upvotes: 0
Reputation: 786081
You can syntax error in your sed command. Use this syntactically correct sed command:
sed -E 's/^([^#]|$)/chr/' /home/me/37.vcf > sp.vcf
OR on Linux:
sed -r 's/^([^#]|$)/chr/' /home/me/37.vcf > sp.vcf
Upvotes: 0
Reputation: 85883
To apply the substitution to only the lines that don't start with a #
:
sed '/^[^#]/s/^/chr/' file > output
Note: the command cat
is for concatenating files, it is useless here.
Upvotes: 3