I have some data on a txt table:

Number        Column1        Column2
  1             abc            zxy
  7             tgh            lkj
  1             rfe            zpo

I want to replace the string in the position of lkj, (always in the 3rd column and line 3). Not replacing arbitrarly lkj. Thinking in another example:

    Number        Column1        Column2
  a             875            172
  a             457            357
  2             986            125

I would like to be extracted the 357 in this case. 3rd column 3rd line. I was trying something involving awkwith a parameter $3 for the column, but I don't know what else to do. Any suggestions?

  • 1
    Do you care about the preservation of the manual alignment using spaces, i.e. is this meant to be a pretty txt file for human consumption? – Wildcard Sep 20 '17 at 3:25

If you want preserve the well alignement use awk with sub function.

awk 'NR==3{sub($3, "string_to_replace")}1' infile.txt

else, you can use below instead

awk 'NR==3{$3="string_to_replace"}1' infile.txt

NR==3 means do the replace if it's 3rd line, 1 is always true condition and telling awk print the line (or that's awk's default action)

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.