AWK program

Hi all,

I have the following problem and hope someone could help me.

I have 184 files, each with 5 columns (c1, c2, c3, c4, c5). I am only interrested in column 5, and would like to paste column 5 from all the 184 files into one file. I have tried the following with two files

awk '{print $5)' file1 file2 | paste -d" " >! a06.txt

The above command line does work, but it pastes colum 5 in one column in the new file (a06.txt). I want it to do it side by side, i.e. c5file1 c5file2, instead of c5file1
c5file2
At the end, I expect to have a file with c5file2 c5file2 c5file3....c5file184.

Thanks for your help!

You have posted this request in the incorrect area of the forums. I'll think about the solution and get back to you once a moderator has moved the thread.

Thanks!

I am new to the forum, and could not figure out where to add new post.

awkliker

thread moved!!!

This smells like homework, but here is some of the answer. First I would set up a loop to go from 1...184 and dump the awk results to a file say 'result.txt'. Next I would use the paste command to put them in your desired format.
But for the way you have listed the problem here is the answer
awk '{print $5}' file1 ...... file184>result.txt;paste -s a06.txt

Sorry it is late here. I made a typo. Here is the answer
awk '{print $5}' file1 ...... file184>result.txt;paste -s result.txt > a06.txt

not tested but I would would

awk '{print $5}' file* >> result.txt

What is the "desired" format?

I like it this is much sweeter than my answer, but the way I read it you still need the paste -s. No need for a loop with this. Nice.

Thank you all for your help!

These two command lines "awk '{print $5}' file1 ...... file184>result.txt;paste -s result.txt > a06.txt" give the following output:
c5file1
c5file2
.
.
.
c5file184

which was what I had before

The format should be as "c5file2 c5file2 c5file3....c5file184", i.e. I need the columns to be side by side.
Thanks again!

if you want the columns side by side you can do

 awk '{printf "%s\t",$0} END '{print}'' file* 

No paste needed.

i think you didn't get his question..
suppose he has two files
file1:
1 2 3 4 5
6 7 8 9 0
1 2 3 4 7

file2:
1 8 2 5 6
1 3 6 7 3
4 2 7 4 6

he want final file as:
5 6
0 3
7 6

Yes, Vidyadhar85. That's exactly the format I am looking format.
file1:
1 2 3 4 5
6 7 8 9 0
1 2 3 4 7

file2:
1 8 2 5 6
1 3 6 7 3
4 2 7 4 6

he want final file as:
5 6
0 3
7 6

Thanks!

"' awk '{printf "%s\t",$0} END '{print}'' file* "" This one still does not work.

Thanks!

awk '{printf "%s ",$5} ' file*  |   tr ' ' '\n' | while read a; do read b; echo "$a $b"; done

i don't think this will work :confused:

Sorry for the late reply (meetings... :frowning: )

(assume awkscript filename = SmellsLikeHomeWork.awk)

{
  if (c!=FILENAME) { c=FILENAME ; i++ ;j=0 }
  a[i,++j] = $5
}


END {
    for( n=1 ; n<=j ; n++)
    {
      for( m=1 ; m<=i ; m++ )
      {
        printf("%s ",a[m,n]);
      }
      printf("\n");
    }
}

Usage: nawk -f SmellsLikeHomeWork.awk file1 file2 file3 file4

This should do:

awk '{print $5}' $(ls -1 file* |sort -n -k1.5) |pr -t -s" " -184

184 is the no. of files. The reason behind the "$(ls -1 file* |sort -n -k1.5)" is to get the files in correct alphabetical order (it skips 'file' and sorts on 1, 2, 3...). If your filenames are not following a regular pattern, then you may need to put them manually in the correct order like this:

awk '{print $5}' f1 file2 filename3 |pr -t -s" " -3

:b::slight_smile:
great.. i couldn't remember that "pr" command :mad:

:slight_smile: I don't think its used often but sometimes its just what you need.