Reputation: 87
I have a file,named score.tsv, the content is much more like:
1 jack 100 good
2 tom 50 notgood
....
and i want to modify the fourth field of the second line "notgood" to "fail" i used
awk -F"\t" '{if (NR==2) $4="fail";print > "temp.tsv"}' score.tsv
it works.
then i realized i need to change the second field of the second line to "lily" too. so i tried
awk -F"\t" '{if (NR==2) $2="lily";print > "temp1.tsv"}' temp.tsv
it doesn't work.
the content of temp1.tsv is
1 jack 100 good
2 tom 50 fail lily
.....
how can i change the fields in one line of a file one by one? thanks
Upvotes: 0
Views: 53
Reputation: 195219
the problem is, in your first command nogood->fail
, you didn't assgin OFS
. so that in your tmp.txt, it is NOT <tab>
separated. so in your 2nd command, the $2
actually didn't exist, awk will append lily
to the 2nd line.
try this two lines:
awk -F"\t" -v OFS="\t" '{if (NR==2) $4="fail";print > "temp.tsv"}' score.tsv
awk -F"\t" -v OFS="\t" '{if (NR==2) $2="lily";print > "temp1.tsv"}' temp.tsv
Upvotes: 3