awk Pattern Match One File to Another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Pattern Match One File to Another
# 1  
Old 07-12-2013
awk Pattern Match One File to Another

I want to read from file 1 and pattern match in file two and print field two from the next line.

File 1:
Code:
 
user1
user2
user3

File 2:
Code:
name=user1
gud=12345
name=user2
gud=32456

I have this pattern hardcoded but can't work out how to pass file 1 to the pattern match:

Code:
awk '/user1/ {getline; print $2}' file2

Output:
Code:
12345

# 2  
Old 07-12-2013
this should work:

Code:
awk -F= 'NR==FNR{n=$2;getline;a[n]=$2;next}{print a[$1]}' file2 file1

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 07-12-2013
Thanks for that.

All I seem to get is blank lines though?
# 4  
Old 07-12-2013
Which OS are you running? On Solaris you might have to use nawk instead of awk
# 5  
Old 07-12-2013
Spot on
Code:
 SunOS vq97eda 5.10 Generic_147440-19 sun4v sparc sun4v

.

Almost there.

Code:
> cat file1
user1
user2
> cat file2
name=user1
gud=123456
name=user2
gud=987654
name=user4
gud=xxxxxxx
> nawk -F= 'NR==FNR{n=$2;a[n]=$2;next}{print a[$1]}' file2 file1
user1
user2

However, what I want is

Code:
123456
987654

# 6  
Old 07-12-2013
you forgot to add 'getline' in the script
# 7  
Old 07-12-2013
What an idiot!

Appreciate patience - works perfectly.

---------- Post updated at 03:14 PM ---------- Previous update was at 02:57 PM ----------

Can I make it a bit more complex. Same logic but with an extra file and and an output depending if they match.

For example: Read file1 get gud from file2 and file3. Compare these. If the same then output to match.out else nonmatch.out.

file3
Code:
name=user1
gud=123456
name=user2
gud=
name=user4
gud=111111

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update value based on pattern match in another file

In the awk, thanks you @RavinderSingh13, for the help in below, hopefully it is close as I am trying to update the value in $12 of the tab-delimeted file2 with the matching value in $1 of the space delimeted file1. I have added comments for each line as well. Thank you :). awk awk '$12 ==... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

Filesystem pattern match in awk

Hi, I'm trying to grep appln processes using its filesystem and also using awk to get accurate results, however when i'm uisng the filesystem in awk statement i'm getting error. Requesting help. ps -eaf | grep ApplnName | awk '/ /opt/xxx/yyy / { print }' Trying with this above code; getting... (7 Replies)
Discussion started by: sam_bd
7 Replies

3. Shell Programming and Scripting

awk command to get file content until 2 occurrence of pattern match

AWK command to get file content until 3 occurrence of pattern match, INPUT FILE: JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2"> <person>Romi</person> <appName>SAP</appName> </custOptIn> ... (4 Replies)
Discussion started by: prince1987
4 Replies

4. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

5. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

6. Shell Programming and Scripting

sum using awk with pattern match

I have a file which has data like this *** Query completed. One row found. *** Query completed. One row found. *** Query completed. One row found. *** Insert completed. 5 rows added. *** Query completed. No rows found. *** Query completed. One row found. *** Query completed. One... (2 Replies)
Discussion started by: sol_nov
2 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

8. Shell Programming and Scripting

pattern match .com in awk script

guys ! I want to search .com,.html files ..... how do I match pattern...? here's wht I hv written if ( $i ~ /.com/ ) even escaping it doesn't help if ( $i ~ /\.com/ ) (2 Replies)
Discussion started by: shreeprabha
2 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

want to pattern match using awk

Hello Friends, My script gives an output like below:- but i only want the red part to be displayed. how to i do that. I am enclosing my shell script after that. id='CCRCWebServerINSTALLDIR' id='AdministrationTools-CINSTALLDIR' id='AdministrationTools-ent-CINSTALLDIR'... (3 Replies)
Discussion started by: asirohi
3 Replies
Login or Register to Ask a Question