LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-22-2010, 10:02 AM   #1
orcaja
Member
 
Registered: Aug 2008
Posts: 92

Rep: Reputation: 1
Smile extracting columns from multiple files with awk


hi everyone!

I'd like to extract a single column from 5 different files and put them together in an output file. I saw a similar question for 2 input files, and the line of code workd very well, the code is:

awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $2}' file1 file2

I added the file3, file4 and file5 at the end, but it doesn't work. Does anyone know what do I have to do?

Thanks in advance.
Joc
 
Old 09-22-2010, 11:19 AM   #2
Vrajgh
Member
 
Registered: Aug 2005
Posts: 65

Rep: Reputation: 31
You could always do:

Code:
cat file1 file2 file3 file4 | awk 'whatever'
 
Old 09-22-2010, 11:58 AM   #3
orcaja
Member
 
Registered: Aug 2008
Posts: 92

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Vrajgh View Post
You could always do:

Code:
cat file1 file2 file3 file4 | awk 'whatever'
This prints one column below the other. Is it possible to print them side by side?

thanks in advance
 
Old 09-22-2010, 01:52 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Try this untested code:
Code:
gawk 'FILENAME!=saved{saved=FILENAME;++nf;maxnr=1}{a[NR,nf]=$2;maxnr=(NR>maxnr)?NR:maxnr} END{for(i=1;i<=maxnr;++1){for(j=1;j<=nf){printf("%s : ", a[i,j], $2} printf("\n")' file1 file2 ... filen
Note that this (if it works) stores the field $2 contents from all input files in memory before printing. The columns are printed with colon (":") delimiters, and no assumption that the files are of equal length is made.

Last edited by PTrenholme; 09-22-2010 at 01:53 PM.
 
Old 09-22-2010, 02:00 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by orcaja View Post
hi everyone!

I'd like to extract a single column from 5 different files and put them together in an output file. I saw a similar question for 2 input files, and the line of code workd very well, the code is:

awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $2}' file1 file2

I added the file3, file4 and file5 at the end, but it doesn't work. Does anyone know what do I have to do?

Thanks in advance.
Joc

How about "cut & paste" (so to speak): ;}

Code:
paste  <(cut -f2 file1)  <(cut -f2 file2)  <(cut -f2 file3)  <(cut -f2 file4)  <(cut -f2 file5)
"Take the second 2 column of all files and place them side by side".


Cheers,
Tink
 
1 members found this post helpful.
Old 09-23-2010, 06:36 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Well I think I am on the same page as PTrenholme but used slightly different qualifiers:
Code:
awk 'FNR==1{f++}{a[f,FNR]=$2}END{for(x=1;x<=FNR;x++){for(y=1;y<ARGC;y++)printf("%s ",a[y,x]);print ""}}' file*
Again this code assumes same number of rows.
 
Old 02-14-2012, 02:41 PM   #7
xijunw
LQ Newbie
 
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Well I think I am on the same page as PTrenholme but used slightly different qualifiers:
Code:
awk 'FNR==1{f++}{a[f,FNR]=$2}END{for(x=1;x<=FNR;x++){for(y=1;y<ARGC;y++)printf("%s ",a[y,x]);print ""}}' file*
Again this code assumes same number of rows.
The best answer that worked very well on my end;

However, does any body can give single-line script to extract different columns from different files?
 
Old 02-14-2012, 11:24 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Please ask your own question and not resurrect such an old thread. You may reference this thread if it helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
AWK - combining multiple columns AlexYZ Programming 5 02-24-2010 08:09 AM
printing multiple columns with awk kdelover Programming 16 12-16-2009 10:10 AM
[SOLVED] extracting multiple files into subfolders ebolamayinga Linux - Software 2 10-02-2009 05:47 PM
AWK/Perl for extracting data from txt file to numerous other files briana.paige Linux - Newbie 2 05-05-2009 10:53 AM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 11:10 AM


All times are GMT -5. The time now is 08:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration