Stephopolis
Stephopolis

Reputation: 1795

file edit- commandline unix

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

Answers (3)

potong
potong

Reputation: 58558

This might work for you (GNU sed):

sed '/^\s*#/!s/^/chr/' file > new_file

Upvotes: 0

anubhava
anubhava

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

Chris Seymour
Chris Seymour

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

Related Questions