How Do You Use awk for String Replacement of Lines where the Word to Be Replaced Is in the Same Position?

Problem scenario
You want to substitute the nth word of a given line. How do you use awk to accomplish this?

Let's assume the lines that you want to modify are in goodfile.txt. To replace the fourth item in the string with "new string", run this command:

cat goodfile.txt | awk '{ print $1 " " $2 " " $3 " new string " $5 " " $6 }'

Leave a comment

Your email address will not be published. Required fields are marked *