Reputation: 174
Seems no one yet stumbled upon my problem,so here it goes.
So sets say,i want to take the third column $3
of every other line 2nd,4th,6th etc
of input file(i know how to do that) and then print the result at the exactly same position in an output file.
I mean,$3
of 2nd
line of input,to $3
of 2nd
line of output and goes on.
I want this,using awk from bash.
Upvotes: 0
Views: 595
Reputation: 41446
Here is how to do it with awk
awk 'FNR==NR {a[NR]=$3;next} FNR%2==0 {$3=a[FNR]}1' firstfile secondfile
This will take data from firstfile
and update secondfile
using column $3
and every second line.
Data is replaced in the same position.
cat firstfile
1 one green
2 two red
3 three blue
4 four orange
5 five yellow
6 six brown
cat secondfile
1 one jan
2 two feb
3 three mars
4 four april
5 five jun
6 six juli
Running awk
command:
1 one jan
2 two red
3 three mars
4 four orange
5 five jun
6 six brown
Too store data back to secondfile
awk 'FNR==NR {a[NR]=$3;next} FNR%2==0 {$3=a[FNR]}1' firstfile secondfile > tmp && mv tmp secondfile
Upvotes: 2
Reputation: 207405
It seems you can't explain what you want very well, so I'll guess and maybe you can say what is wrong and it will get solved by process of elimination...
Given an input file called file
, like this:
Line 0,field2,field3,90
Line 1,field2,field3,80
Line 2,field2,field3,82
Line 3,field2,field3,21
Line 4,field2,field3,85
The following awk will print every other line's field3 in the same place as it found it, but without the other stuff around it:
awk -F, 'NR%2{print ",,"$3"\n,,";}' file
Output:
,,field3
,,
,,field3
,,
,,field3
,,
Upvotes: 1