-1

I am wondering if there is a simple way using sed or awk to replace a string in a specific column and in multiple lines with another string. The string repeats in other columns but I would like the string in a specific column to be replaced while preserving the inconsistent number of white spaces between columns. For example, I'm hoping to replace 'A' in column 3, lines 2-4 to 'B', while preserving the same whitespaces between columns.

 1  2      A     4    A     B       C         A  E    F  G   7  8    9 
 2  11     A     4    A     B       C         A  E    F  G   7  8    9 
 3  12     A     4    A     B       C         A  E    F  G   7  8    9 
 4  13     A     4    A     B       C         A  E    F  G   7  8    9 
 5  14     A     4    A     B       C         A  E    F  G   7  8    9 
 6  15     A     4    A     B       C         A  E    F  G   7  8    9
 .
 .

to

 1  2      A     4    A     B       C         A  E    F  G   7  8    9 
 2  11     B     4    A     B       C         A  E    F  G   7  8    9 
 3  12     B     4    A     B       C         A  E    F  G   7  8    9 
 4  13     B     4    A     B       C         A  E    F  G   7  8    9 
 5  14     A     4    A     B       C         A  E    F  G   7  8    9 
 6  15     A     4    A     B       C         A  E    F  G   7  8    9
 .
 .

Thanks!

kwaldner
  • 95
  • 1
  • 6

1 Answers1

1

You have to add the condition for the line range, before the action you have to do, NR is the record number, usually the line number.

awk 'NR>=2 && NR<=4{sub("A","B",$3)} 1' file
1 2 A 3
2 3 B 4
3 4 B 5
4 5 B 6
5 6 A 7

1 at the end means to do the default action, which is to print every line. Also sub is one of the GNU Awk string functions.


UPDATE

For your updated requirements, doing the replacement on $0 without splitting to fields, will work. Also this sub replaces only the first occurenc of A in line.

awk 'NR>=2 && NR<=4{sub("A","B",$0)} 1' file
thanasisp
  • 5,855
  • 3
  • 14
  • 31
  • 1
    thanks! and I am wondering what the number 1 represents at the end of the quotations? – kwaldner Sep 02 '20 at 00:22
  • Thanks! and is there something that I could add to preserve the spaces between columns? If the columns are separated by two spaces, it seems that the command would change it to one. – kwaldner Sep 02 '20 at 00:28
  • Thanks! If the whitespaces between each column is not fixed, so there are 2 spaces for some, 3 spaces for some, etc, is there a simple command for it? – kwaldner Sep 02 '20 at 00:41
  • I'm wondering if there is another way, for example, instead of identifying the string A in the 3rd column, is there a way to identify A by assigning it as the 5th element from the left (including whitespaces) and solely replacing it. – kwaldner Sep 02 '20 at 00:49
  • 1
    There are many ways, it depends on the input file. If you have a specific case you want to preserve whitespaces in output, while replacing a column, I suggest posting a new question with a representative example input, you will get answers with different ways to do it. – thanasisp Sep 02 '20 at 00:51
  • Also, this is a helpful read for preserving spaces: https://stackoverflow.com/questions/20835437/how-to-preserve-the-original-whitespace-between-fields-in-awk – thanasisp Sep 02 '20 at 00:55
  • Thanks! I've edited the question, sorry about the earlier confusion – kwaldner Sep 02 '20 at 00:58