The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to extract first column with a specific character selamba_warrior Shell Programming and Scripting 3 05-22-2008 05:14 AM
How to extract only first column from the file selamba_warrior Shell Programming and Scripting 11 05-21-2008 02:52 AM
column extract help cvm Shell Programming and Scripting 1 04-24-2008 04:19 PM
Extract column data from File sudheshnaiyer UNIX for Dummies Questions & Answers 3 10-11-2007 09:52 PM
extract column based on name t27 UNIX for Dummies Questions & Answers 3 08-29-2007 01:04 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-28-2008
solracq solracq is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 8
How to extract a column from two different files in AWK?

Hi guys,

I need help in extracting one column of numbers from two different files and display it in a output file. In specific, I want to extrac the column no.2 ($2) from each file, file1.txt, file2.txt. Then place both extracted columns in a one file, out.txt.

the line command I use to call the AWK code and the files is this:
awk -f code.awk file1.txt file2.txt > out.txt

code.awk I have is (and is worng! ):
#Extracting columns
#
BEGIN {}
#{print $2 < file1.txt , $2<file2.txt}
END {}

I appreciate a lot your kind help,

solracq,
  #2 (permalink)  
Old 04-28-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 741
Question Show output

Do you want the columns side by side or one after another in the output. Can you show how the output looks like.
  #3 (permalink)  
Old 04-28-2008
SandmanCL SandmanCL is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 9
most likely you'll end up with a solution involving paste and awk.

If each file has two columns the command would be

Quote:
paste file1 file2 | awk '{print $1 " " $3}'
  #4 (permalink)  
Old 04-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If your shell supports it:

Code:
paste <(cut -f2 file1.txt) <(cut -f2 file2.txt)
If you can't get the <(...) syntax to work, you will need temporary files, at least for one of the cuts.

Code:
cut -f2 file1.txt >tmp
cut -f2 file2.txt | paste tmp -
cut and paste normally work on tab-delimited input; if you have variable amounts of spaces as separators, awk is definitely the way to go.
  #5 (permalink)  
Old 04-29-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
Pure awk:

side by side:
Code:
 awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $2}' file1 file2
concatenated on one column:
Code:
 awk '{print $2}' file1 file2
  #6 (permalink)  
Old 04-29-2008
solracq solracq is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 8
Quote:
Originally Posted by shamrock View Post
Do you want the columns side by side or one after another in the output. Can you show how the output looks like.
hi Shamrock,

the output should be the column#2 of file 1, Tab, the column #2 of file 2

file1
1 2
2 3
3 4

file2
5 8
6 9
7 10

output
2 8
3 9
4 10

thanks!,
solracq
  #7 (permalink)  
Old 04-29-2008
solracq solracq is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 8
Quote:
Originally Posted by ripat View Post
Pure awk:

side by side:
Code:
 awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $2}' file1 file2
concatenated on one column:
Code:
 awk '{print $2}' file1 file2
ripat,

IT WORKS...!
THANKS A LOT !!!

solracq
p.s. also thx to to the ppl who answered my question!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0