Need Help with Joining Command or AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help with Joining Command or AWK
# 8  
Old 04-15-2009
Try this:

Code:
awk -F@ 'NR==FNR{a[$1]=$2;next}
a[$1]{print $0, a[$1]}' OFS="@" file2 file1

Use nawk or /usr/xpg4/bin/awk on Solaris

Regards
# 9  
Old 04-15-2009
this will also do i guess
Code:
awk -F"@" 'FILENAME=="file2"{A[$1]="@"$2}
FILENAME=="file1"{(A[$1]){print $0A[$1]}}' file2 file1

# 10  
Old 04-15-2009
Quote:
Originally Posted by Franklin52
Try this:

Code:
awk -F@ 'NR==FNR{a[$1]=$2;next}
a[$1]{print $0, a[$1]}' OFS="@" file2 file1

Use nawk or /usr/xpg4/bin/awk on Solaris

Regards
this worked great however it only grab the 2nd field on the 2nd file. How do I get it so that I get everything on the 2nd file if it matches the 1st field of the first file. The 2nd file has 6 field in it. Thanks for the help.
# 11  
Old 04-15-2009
Quote:
Originally Posted by crazyhpux
this worked great however it only grab the 2nd field on the 2nd file. How do I get it so that I get everything on the 2nd file if it matches the 1st field of the first file. The 2nd file has 6 field in it. Thanks for the help.
changes are as followed
Code:
awk -F@ 'NR==FNR{a[$1]=$2$3$4$5$6;next}
a[$1]{print $0, a[$1]}' OFS="@" file2 file1

# 12  
Old 04-15-2009
Quote:
Originally Posted by vidyadhar85
changes are as followed
Code:
awk -F@ 'NR==FNR{a[$1]=$2$3$4$5$6;next}
a[$1]{print $0, a[$1]}' OFS="@" file2 file1

awesome...thank you guys so much. you are a lifesaver.
# 13  
Old 04-15-2009
For join to work you must sort both input files on the field you want compared.

Code:
sort file1 > file1.sort;mv file1.sort file1
sort file2 > file2.sort;mv file2.sort file2
join -1 1 -2 1 -t@ -o 1.1,1.2,1.3,1.4,1.5,2.2 file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue with awk when joining two files when field has '-' hyphen

Dear Community; I need to join two files but I am facing issues. 1st file has multiple columns. Primary (1st) columns has unique values. There are other columns out of which some has non-ascii characters as well (other language). Example File below: 1-1001JRL,BiRecurring... (5 Replies)
Discussion started by: mystition
5 Replies

2. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

3. Shell Programming and Scripting

Joining files using awk not extracting all columns from File 2

Hello All I'm joining two files using Awk by Left outer join on the file 1 File 1 1 AA 2 BB 3 CC 4 DD File 2 1 IND 100 200 300 2 AUS 400 500 600 5 USA 700 800 900 (18 Replies)
Discussion started by: venkat_reddy
18 Replies

4. Shell Programming and Scripting

awk --> math-operation in data-record and joining with second file data

Hi! I have a pretty complex job - at least for me! i have two csv-files with meassurement-data: fileA ...... (2 Replies)
Discussion started by: IMPe
2 Replies

5. Shell Programming and Scripting

Joining broken lines with awk or perl

Hi, I have a huge file with sql broken statements like: PP3697HB @@@@0 <<<<<<Record has been deleted as per PP3697HB>>>>>> FROM sys.xtab_ref rc,sys.xtab_sys f,sys.domp ur WHE RE rc.milf = ur.milf AND rc.molf = f.molf AND ur.dept = 'SWIT'AND ur .department = 'IND' AND share = '2' AND... (4 Replies)
Discussion started by: som.nitk
4 Replies

6. Shell Programming and Scripting

Joining lines in a text file using AWK or SED

Hi All I'm struggling a bit here :( I need a way of joining lines contained in a text file. I've seen numerous SED and AWK examples and none of them seem to be working for me. The text file has 4 lines: DELL1427 DOC 30189342 79 Now bear with me on this one as I'm actually... (4 Replies)
Discussion started by: huskie69
4 Replies

7. Shell Programming and Scripting

awk joining lines

Hello, I'm trying to write a piece of code in awk, which should be able recognize by some regexps two lines and then join them together (maybe write them without \n would be enough, I don't know).. the thing is that every line in the file i'm working with starts with some number, for example: ... (4 Replies)
Discussion started by: midin
4 Replies

8. Shell Programming and Scripting

Joining Two Files Using Awk

Hi All, I am new to awk program. But i have got some assignment on awk. The problem is: i have two files file1 and file2. Both files have same structure. First i have to join both files on filed1,field2 and field3 and then for matching records i want to perform some calculation like:... (1 Reply)
Discussion started by: Jeetuibm
1 Replies

9. Shell Programming and Scripting

joining command results, and substitution

Hello community I'd like to join to command results and put it to the same line in one file, how can I do that? file: a.txt so when I put Date '+%H:%M' and echo date '+%D' in the file appears 14:44 01/05/08 not 14:44 01/05/08 I like to know how can I make a substituion of a whole... (6 Replies)
Discussion started by: ncatdesigner
6 Replies

10. Shell Programming and Scripting

Awk error for joining records with CR/newline

Is there any way to remove carriage retuns between the records? These carriage returns are created in an excel cell by using Alt+enter, this is similar to new line... We have input records separated by TABS and have carriage returns as below: 123 456 789 ABC "1952.00" 678 "abcdef ghik... (5 Replies)
Discussion started by: acheepi
5 Replies
Login or Register to Ask a Question