I have the following content in a file.

$ cat file.txt

I would like to change the "path" on the 6th field to "test-path".


I tried with following, but it didn't work.

$ awk -F, -v needle='path' -v replacement='test-path' 'BEGIN{ OFS = FS; } (NR > 1 && $6 == needle) { $6 = replacement; } 1' file.txt
  • @Prvt_Yadv Thanks for pointing out, I will add the filename. – smc Mar 28 at 12:07
  • @Prvt_Yadv because there is an occurrence of the same string in 1st column, and I don't want to change that. I just want to change the 6th column content. – smc Mar 28 at 12:09
  • Or you can assign full path instead of partial to variable. – Prvt_Yadv Mar 28 at 12:09
  • @steeldriver I thought the same, I was not sure which one would do the regex part, thanks for pointing out ~, I will try the same. – smc Mar 28 at 12:10
  • @steeldriver Unfortunately ~ didn't work. It is clear that I have to use regex to replace the part of the string in a column, but not sure about the exact way. – smc Mar 28 at 12:18

Try this,

awk -F ',' -v OFS=',' '{gsub("path","test-path",$6)}1' file.txt


To substitute path with test-path only in 6th field


Tried with below command

$ sed "s/\/path/\/test-path/" filename
  • The risk here is that the sed command you've used would replace any and all "/path" strings to "/test-path" -- and not just the ones in field 6, as required. – Jeff Schaller Mar 31 at 14:35

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.