replace field on certain line with awk

Replace second field in line that start with root string

awk -F':' '/^root/{sub($2, "abc")}1' /etc/passwd
awk -F':' '/^root/{sub($2, "abc"); print}' /etc/passwd
awk -F':' '/^root/{sub($2, "abc")}1' /etc/passwd > newFile
awk -F':' '/^root/{sub($2, "abc")}/^root/' /etc/passwd >> newFile

Replace second field in line that have root string in first field

awk -F':' '$1~"root$"{sub($2, "abc")}1' /etc/passwd
awk -F':' '$1~"root$"{sub($2, "abc"); print}' /etc/passwd
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s