How to extract a column from two different files in AWK?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract a column from two different files in AWK?
# 1  
Old 04-28-2008
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  
Old 04-28-2008
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  
Old 04-28-2008
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  
Old 04-28-2008
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  
Old 04-29-2008
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

This User Gave Thanks to ripat For This Post:
# 6  
Old 04-29-2008
Quote:
Originally Posted by shamrock
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  
Old 04-29-2008
Quote:
Originally Posted by ripat
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I extract specific column in multiple csv files?

file1: Name,Threshold,Curr Samples,Curr Error%,Curr ART GETHome,100,21601,0.00%,47 GETregistry,100,21592,0.00%,13 GEThomeLayout,100,30466,0.00%,17 file2: Name,Threshold,Curr Samples,Curr Error%,Curr ART GETHome,100,21601,0.00%,33 GETregistry,100,21592,0.00%,22... (6 Replies)
Discussion started by: Raghuram717
6 Replies

2. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

3. Shell Programming and Scripting

awk to extract value from column using variable

I am having trouble extracting the value in the columns declared in a variable. I have tried several different variation of awk but both give me the column number and not the actual value of that column. Any suggestions? Neither of the "extract" variables below are performing as desired ... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

4. UNIX for Dummies Questions & Answers

awk - Extract 4 lines in Column to Rows Tab Delimited between tags

I have tried the following to no avail. xargs -n8 < test.txt awk '{if(NR%6!=0){p=""}else{p="\n"};printf $0" "p}' Mod_Alm_log.txt > test.txt I have tried different variations of the above, the problem is mixes lines together. And it includes the tags "%a and %A" I need them to be all tab... (16 Replies)
Discussion started by: mytouchsr
16 Replies

5. Shell Programming and Scripting

Compare files & extract column awk

I have two tab delimited files as given below: File_1: PV16 E1 865 2814 1950 PV16 E2 2756 3853 1098 PV16 E4 3333 3620 288 PV16 E5 3850 4101 252 PV16 E6 83 559 477 PV16 E7 562 858 297 PV16 L2 4237 5658 ... (10 Replies)
Discussion started by: vaibhavvsk
10 Replies

6. Shell Programming and Scripting

extract column with awk

This is a example TXT file: ID CDR1 CDR2 CDR3 1 CDR1 CDR3 2 CDR1 CDR2 CDR3 3 CDR3 What I want to do is extract the fourth column with awk, however, there are some blank space in some field. I get wrong result when it print out awk result. awk '{print $1,$4}'... (8 Replies)
Discussion started by: xshang
8 Replies

7. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

8. Shell Programming and Scripting

awk/sed to extract column bases on partial match

Hi I have a log file which has outputs like the one below conn=24,196 op=1 RESULT err=0 tag=0 nentries=9 etime=3,712 dbtime=0 mem=486,183,328/2,147,483,648 Now most of the time I am only interested in the time ( the first column) and a column that begins with etime i.e... (8 Replies)
Discussion started by: pkabali
8 Replies

9. Shell Programming and Scripting

extract data with awk from html files

Hello everyone, I'm new to this forum and i am new as a shell scripter. my problem is to have html files in a directory and I would like to extract from these some data that lies between two different lines Here's my situation <td align="default"> oxidizability (mg / l): data_to_extract... (6 Replies)
Discussion started by: sbobotex
6 Replies

10. UNIX for Dummies Questions & Answers

AWK, extract data from multiple files

Hi, I'm using AWK to try to extract data from multiple files (*.txt). The script should look for a flag that occurs at a specific position in each file and it should return the data to the right of that flag. I should end up with one line for each file, each containing 3 columns:... (8 Replies)
Discussion started by: Liverpaul09
8 Replies
Login or Register to Ask a Question