bash - Print result to every second line of output file in awk -
seems no 1 yet stumbled upon problem,so here goes.
sets say,i want take third column $3
of every other line 2nd,4th,6th etc
of input file(i know how that) , print result @ same position in output file.
mean,$3
of 2nd
line of input,to $3
of 2nd
line of output , goes on.
i want this,using awk bash.
here how awk
awk 'fnr==nr {a[nr]=$3;next} fnr%2==0 {$3=a[fnr]}1' firstfile secondfile
this take data firstfile
, update secondfile
using column $3
, every second line.
data replaced in 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 1 jan 2 two red 3 three mars 4 four orange 5 five jun 6 six brown
too store data secondfile
awk 'fnr==nr {a[nr]=$3;next} fnr%2==0 {$3=a[fnr]}1' firstfile secondfile > tmp && mv tmp secondfile
Comments
Post a Comment