use awk print statement for two files

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use awk print statement for two files
# 1  
Old 08-03-2011
use awk print statement for two files

is there a way to use the awk print statement on two files at once? I would like to take two columns from one file, and one column from another file and print them as consecutive columns to a third file. Seems in:

file 1
1  a
2  b
3  c
4  d
5  e

file 2
1  t 
2  u
3  v
4  w
5  x

output file
1  a  t
2  b  u
3  c  v
4  d  w
5  e  x

who has an idea how to do this? With awk or other?

Last edited by radoulov; 08-03-2011 at 05:12 AM.. Reason: Code tags!
# 2  
Old 08-03-2011
paste file1 file2 | awk '{print $1, $2, $4}'

# 3  
Old 08-03-2011
With awk:

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

If the files are sorted:

join file1 file2

# 4  
Old 08-03-2011
Thanks a lot, I think that does the job.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Explanation of print statement - awk

Hi, i'm just after a simple explanation of how the following awk oneliner works. gawk -F"," '{for(i=m;i<=n;i++)printf "%s" OFS,$i; print x}' m=1 n=70 OFS=, input.csv > output.csv In particular i'm trying to understand how the two print statements work? How is the "x" variable being assigned... (3 Replies)
Discussion started by: theflamingmoe
3 Replies

3. Shell Programming and Scripting

Compare two files and print using awk

I have 2 files: email_1.out 1 2 3 data_1.out <tr> 1 MAIL # 1 TO src_1 </tr> <tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> <tr><td... (1 Reply)
Discussion started by: sol_nov
1 Replies

4. Shell Programming and Scripting

Awk find in columns with "if then" statement and print results

I have a file1.txt file1.txt F-120009210","Felix","U-M-F-F-F-","white","yes","no","U-M-F-F-F-","Bristol","RI","true" F-120009213","Fluffy","U-F-","white","yes","no","M-F-","Warwick","RI","true" U-120009217","Lity","U-M-","grey","yes","yes","","Fall River","MA","true"... (4 Replies)
Discussion started by: charles33
4 Replies

5. Shell Programming and Scripting

AWK help print dirs with files in it

Hi, I'm writing some start of day checks for my work. I want to check some dirs for files that have been created longer than 10 mins ago and not been transfered. I've already used a find command to write a list of files that meet this criteria to a log called sod.log i.e. ... (1 Reply)
Discussion started by: elcounto
1 Replies

6. Shell Programming and Scripting

Sysdate inside awk print statement

Hi, I am using awk statement to extract data from a file and write a new file with certain columns rearranged and few hard coded values added to new file. Now i need to add a column with sysdate. can i do that inside the awk print statement? Now: nawk ' /^3/ BEGIN {FS=","}... (2 Replies)
Discussion started by: selvankj
2 Replies

7. UNIX for Dummies Questions & Answers

Print files with spaces using awk

When I use: find . -ls | awk 'BEGIN { OFS = ";"} {print $1,$2,$11}'I get a nice result, yet the files with spaces in it show only the first word and all other characters after the blank space are not printed. e.g. 'file with a blank space' is printed file 'file_with a blank space' is... (7 Replies)
Discussion started by: dakke
7 Replies

8. Shell Programming and Scripting

AWK print lines into multiple files

Hi, i have an input text file like this: Student 1 maths science = Student 2 maths science = Student 3 maths science i would like to print each student information into separate files, each student id is separated by "=". (1 Reply)
Discussion started by: saint2006
1 Replies

9. Shell Programming and Scripting

Print to 2 files in awk if statement

Hi all, I have some code like this awk -F, '{ if ($1==3) print $2 > "output_file" print "1" > "new_file" }' "input_file" When I check output_file this has the correct values in it. However the new_file has 1 in it for every line in the input_file. If the input file has 20 lins then... (2 Replies)
Discussion started by: Donkey25
2 Replies

10. Shell Programming and Scripting

awk print fields to multiple files?

I am trying to print the output of a command to two separate files. Is it possible to use awk to print $1 to one file and $2 to another file? Thanks in advance! (1 Reply)
Discussion started by: TheCrunge
1 Replies
Login or Register to Ask a Question