1

I have 2 files having different number of columns and with huge number of rows:

bash-3.00$ cat fileA 
a1,a2,a3,a4,a5,a6,a7,a8,a9
q,w,,,,y,u,,
a,z,,,,q,n,,
.........................
z,p,,,,w,e,,

PS1: a1 - a9 are the headers separated by comma (,). PS2: dot signs (.) means that there are many lines between the two lines

bash-3.00$ cat fileB 
b1      b2
f       t
a       p
m       n
..........      
m       y
t       o

PS: b1 - b2 are the headers and separated by space.

I want to append the a2 and a6 columns of fileA to the contents of b1 and b2 columns. That is, a2 column will be appended to the b1 column; a6 column will be appended to the b2 column.

So, the output file would be as follows:

bash-3.00$ cat output 
a1,a2,a3,a4,a5,a6,a7,a8,a9
q,w,,,,y,u,,
a,z,,,,q,n,,
.........................       
z,p,,,,w,e,,
,f,,,,t,,,
,a,,,,p,,,
,m,,,,n,,,
.........................
,m,,,,y,,,
,t,,,,o,,,

How can I do that by a simple awk command?

  • Can you please post terminal output as formatted text, not as pictures? – chaos Jul 31 '15 at 13:43
1

It looks like what you want is to i) append the contents of file1 to file2 and ii) change the format of file2 to match that of file one. That is not what your question is actually asking for but it's what you show in your output. If I misunderstood, please edit your question and clarify.

So, to do that, you could simply do:

awk 'NR>1{printf ",%s,,,,%s,,,\n",$1,$2}' file2 >> file1 

That will make file1 the output file. If you want to keep file1, you could do:

( cat file1; awk 'NR>1{printf ",%s,,,,%s,,,\n",$1,$2}' file2 ) > out

Or, you could do the whole thing in awk (this is just a simplified version of Archemar's solution):

awk 'NR==FNR{print; next} FNR>1{printf ",%s,,,,%s,,,\n",$1,$2}' file1 file2 > out
1

try

awk 'FNR == NR { print ; next ; } 
     NR > FNR && FNR > 1 { 
          split($0,A) ; 
          printf ",%s,,,,%s,,,\n",A[1],A[2] ;  }' f1 f2 

(can be in one line, I break it to be more readable)

where

  • FNR == NR { print ; next ; } copy lines from first file
  • NR > FNR && FNR > 1 select line from second file without header
  • split($0,A) ; put two var in an array (A)
  • printf ",%s,,,,%s,,,\n",A[1],A[2] ; and print

edit

tested with f1

a1,a2,a3,a4,a5,a6,a7,a8,a9
q,w,,,,y,u,,
a,z,,,,q,n,,
.........................
z,p,,,,w,e,,

and f2

b1  b2
f   t
a   p
m   n
m   y
t   o

run :

awk 'FNR == NR { print ; next ; } NR > FNR && FNR> 1 { split($0,A) ; printf ",%s,,,,%s,,,\n",A[1],A[2] ;  }' f1 f2

result:

a1,a2,a3,a4,a5,a6,a7,a8,a9
q,w,,,,y,u,,
a,z,,,,q,n,,
.........................
z,p,,,,w,e,,
,f,,,,t,,,
,a,,,,p,,,
,m,,,,n,,,
,m,,,,y,,,
,t,,,,o,,,
  • Thanks for your response but it did not help. The output file seems to be empty: bash-3.00$ awk 'FNR == NR { print ; next ; } > NR > FNR && FNR > 1 { > split($0,A) ; > printf ",%s,,,,%s,,,\n",A[1],A[2] ; }' fileA fileB > output bash-3.00$ cat output bash-3.00$ – Murat Jul 31 '15 at 14:12
  • @Murat that's not what he told you to run. Make sure you copy/paste the solution directly into the terminal. Running what you show in your comment should have produced error messages. – terdon Jul 31 '15 at 14:17
  • edited to add the one line solution. – Archemar Jul 31 '15 at 14:18
  • @Archemar why are you splitting $0? awk already does that for you; also, there's no need for NR > FNR, the next does that already. See the version in my answer. – terdon Jul 31 '15 at 14:19
  • @terdon true, as per spliting $0 : I was mislead by commas, and FNR is quiet new in awk. – Archemar Jul 31 '15 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.