What I would like to do is search for lines where the first column does not begin with 'rs' or 'chr' THEN if those lines begin with a number, append 'chr' to the first column value, otherwise leave as it was - no appending.

I have the following code:

awk '((!($1 ~ /rs/ || $1 ~ /chr/)) && $1 ~ /^[[:0-9:]]|$/) {$1 = "chr"$1}1' filename > newfilename

This is good but appends 'chr' to all first column values that do not begin with 'rs' or 'chr'. There are some values in this column that I do not want to change and these all begin with letters (a-z). I only want to change the values which start with numbers (0-9).



As already mentioned in the comments by Fiximan, looking for a digit automatically filters out 'rs' and 'chr'. So if you want to prepend lines starting with a digit with 'chr' you can do the following:

awk '{if ($1 ~ /^[:0-9:]/) printf "chr%s\n", $0; else printf "%s\n", $0;}' filename > newfile

using sed

sed 's/^[0-9]/chr&/' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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