2

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?

1
  • This is almost unreadable, please reformat your question with code tags. Commented Oct 7, 2016 at 16:55

2 Answers 2

4

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
2

Briefly, the one-liner:

awk '$1=$1"N-H"'
3
  • This will rebuild the whole record with space as new FS. Commented Oct 7, 2016 at 17:03
  • @heemayl I don't see any other field separation in the sample data. Commented Oct 7, 2016 at 17:20
  • that's what $OFS is for - awk -F, 'BEGIN{OFS=","}$28=","$28' Commented Aug 29, 2024 at 12:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.