1

I have a file which look like this:

A3 124.406526 8.281803
A3' 125.000684 8.389677
K4 123.072842 8.166373
K4' 120.092553 8.269080

I want to add characters in the 1st column, and the file will should be like:

A3N-H 124.406526 8.281803
A3'N-H 125.000684 8.389677
K4N-H 123.072842 8.166373
K4'N-H 120.092553 8.269080

How can I do this in a easy way by awk or sed?

  • This is almost unreadable, please reformat your question with code tags. – maulinglawns Oct 7 '16 at 16:55
3

With sed:

sed -E 's/^([^[:blank:]]+)/\1N-H/' file.txt
  • ([^[:blank:]]+) matches the first field (^), and put in only captured group

  • In the replacement, N-H is appended to the first field, \1N-H

With awk:

awk '{sub("$", "N-H", $1)}; 1' file.txt 
  • sub("$", "N-H", $1) substitute the end of first field ($) with N-H, this necessarily means an append operation, in place

  • 1 is just a placeholder for true so that the (modified) record is printed

Example:

% cat file.txt                               
A3 124.406526 8.281803
A3' 125.000684 8.389677
K4 123.072842 8.166373
K4' 120.092553 8.269080

% sed -E 's/^([^[:blank:]]+)/\1N-H/' file.txt
A3N-H 124.406526 8.281803
A3'N-H 125.000684 8.389677
K4N-H 123.072842 8.166373
K4'N-H 120.092553 8.269080

% awk '{sub("$", "N-H", $1)}; 1' file.txt    
A3N-H 124.406526 8.281803
A3'N-H 125.000684 8.389677
K4N-H 123.072842 8.166373
K4'N-H 120.092553 8.269080
1

Briefly, the one-liner:

awk '$1=$1"N-H"'
  • This will rebuild the whole record with space as new FS. – heemayl Oct 7 '16 at 17:03
  • @heemayl I don't see any other field separation in the sample data. – Kaz Oct 7 '16 at 17:20

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.