awk searching between two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk searching between two files
# 1  
Old 03-12-2008
MySQL awk searching between two files

hi there,
im writing some script in awk; in few words i have a list from router (mac address- ip address) and the second list with only the mac addresses.
the thing is that i want search list from router for the first mac address; if found - print the ip address, if not print error; then search the router list for the second mac address, if found print ip.. etc.
summarize: i have two files, router and macs like this (respectivly)
43:43:43:43:43 10.0.0.3
43:43:43:43:45 10.0.0.4
43:43:43:43:44 10.0.0.5
43:43:43:43:46 10.0.0.6
and
43:43:43:43:43
43:43:43:43:46

and as result im looking for:
43:43:43:43:43 10.0.0.3
43:43:43:43:46 10.0.0.6

im doing it in this way:
cat router | awk '{
getline tmp < macs
if ($1 = tmp) print
}'

i know, it is wrong, that is why im posting Smilie

any ideas?

cheers.
# 2  
Old 03-12-2008
Given your example output and you explanation:

Code:
$ cat file1
43:43:43:43:43 10.0.0.3
43:43:43:43:45 10.0.0.4
43:43:43:43:44 10.0.0.5
43:43:43:43:46 10.0.0.6
$ cat file2
43:43:43:43:43
43:43:43:43:46

it's not clear if you want this:

Code:
$ nawk 'NR==FNR{x[$1];next}$1 in x' file2 file1
43:43:43:43:43 10.0.0.3
43:43:43:43:46 10.0.0.6

this:

Code:
$ nawk 'NR==FNR{x[$1];next}$1 in x||$0="not found"' file2 file1
43:43:43:43:43 10.0.0.3
not found
not found
43:43:43:43:46 10.0.0.6

this:

Code:
$ nawk 'NR==FNR{x[$1];next}($1 in x&&$0=$2)||$0="not found"' file2 file1
10.0.0.3
not found
not found
10.0.0.6

this:

Code:
$ nawk 'NR==FNR{x[$1];next}($1 in x&&$0=$2)||$0=$1" not found"' file2 file1
10.0.0.3
43:43:43:43:45 not found
43:43:43:43:44 not found
10.0.0.6

or this:

Code:
$ nawk 'NR==FNR{x[$1];next}$1 in x||$0=$1" not found"' file2 file1
43:43:43:43:43 10.0.0.3
43:43:43:43:45 not found
43:43:43:43:44 not found
43:43:43:43:46 10.0.0.6

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

Or just this, of course Smilie

Code:
$ fgrep -f file2 file1
43:43:43:43:43 10.0.0.3
43:43:43:43:46 10.0.0.6


Last edited by radoulov; 03-12-2008 at 02:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching value in table through awk

I need to create one script in which I want to search in txt file , this txt file is having 10 columns , I want to check 4th column value if "BOY" & 10th column value =>500 it will print 4th column row value ,1st column row value & 10th column row value & store the same value in one file as... (2 Replies)
Discussion started by: dravi_laxmi
2 Replies

2. Shell Programming and Scripting

Searching multiple patterns using awk

Hello, I have the following input file: qh1adm 20130710111201 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qx1adm 20130711151154 : tp count QX1 u6 -Dsourcesystems=B17,E17,EE7 qh1adm 20130711151155 : tp import all... (7 Replies)
Discussion started by: kcboy
7 Replies

3. Shell Programming and Scripting

awk: searching for non-breaking-space

This code shal search for the non-breaking space 0xA0 though it returns the error "fatal: attempt to use scalar 'nbs' as array" Can somebody help? awk --non-decimal-data -v nbs="0xA0" '{if($0 in nbs) {print FILENAME, NR}}' *.txt (1 Reply)
Discussion started by: sdf
1 Replies

4. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

5. Shell Programming and Scripting

awk searching

hi all, Please can you help me with the awk searching where: I have a master file where I need the string $12. the string $ 12 varies between 3 and 4 characters and I need to bring only the characters ending in 68. try this, but is so bad my search i know awk-F, '(if... (8 Replies)
Discussion started by: manzi
8 Replies

6. Shell Programming and Scripting

Searching using awk - Help required

Hi... I am working on script to search some records in a file based on certain fields and each record is a ASCII fixed size. I was using awk to search based on certain condition. But the length of the record is too much that awk is giving syntax error near unexpected token `(' Request... (5 Replies)
Discussion started by: ysrikanth
5 Replies

7. Shell Programming and Scripting

awk and searching within a block ?

Hi, I wonder if anybody could help. How do i awk out (or indeed using another utility) a particular value that exists within a defined block, for example if i have a file that looks like the one below and i want to get at the "Product Serial" for the block referring to "mb.fru" (bolded and coloured... (4 Replies)
Discussion started by: rethink
4 Replies

8. Shell Programming and Scripting

Searching word in a file with awk

Hello everyone! I use a script to query for installed packages with yum (I use RHEL 4 with yum installed) and the output is redirected to a file. My script scan this file to find a package and the version of it. The script works fine until I search for a package name with special characters.... (3 Replies)
Discussion started by: studieu
3 Replies

9. Shell Programming and Scripting

problem while searching in a file by 'AWK'

Hi , i am writing a script in which i am using following command to grep some string and then storing it's output to v, as like below :- v=$(awk -F, '{ if ( $NF ~ /DEV/ ) print $0 "_BLD01";else print $0 "_RC01" }' mytest) Here i am facing following issues:- 1. it is searcing DEV in the... (1 Reply)
Discussion started by: inderpunj
1 Replies

10. Shell Programming and Scripting

Searching for + sign in a string using awk

Hi All, My query is: I have string say xyz+ how to determine that whether it ends with a + sign or not using awk command. (5 Replies)
Discussion started by: satyajit2512
5 Replies
Login or Register to Ask a Question