Visit Our UNIX and Linux User Community


grep and awk help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep and awk help
# 1  
Old 09-23-2009
grep and awk help

I have 2 files

file1:

Code:
192.168.1.1 ABC
192.168.1.2 AAA

file 2:

Code:
BBB 192.168.1.1
CCC 192.168.1.2
DDD 192.168.1.3

Output:

Code:
192.168.1.1 ABC BBB
192.168.1.2 AAA CCC

The basic thing I want to do is that I want to match ip addresses and if they match, print the matching lines from file1 and append the first field of the matching lines from file2.
I am trying to use grep -f and awk to achieve this, but I can't get the output right.

Last edited by streetfighter2; 09-23-2009 at 02:13 PM.. Reason: code tags, PLEASE!
# 2  
Old 09-23-2009
192.168.1.1 appears two times in file2. How d you want to handle this case?
# 3  
Old 09-23-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 4  
Old 09-23-2009
Watch what you wish for, it just might come true Smilie
Code:
awk 'NR==FNR{a[NR$2]=$1;next}{print $0,a[FNR$1]}' file2 file1

# 5  
Old 09-23-2009
Quote:
Originally Posted by ripat
192.168.1.1 appears two times in file2. How d you want to handle this case?
Sorry that was a typo on my part. I edited my post to address that.

---------- Post updated at 12:31 PM ---------- Previous update was at 12:14 PM ----------

Quote:
Originally Posted by danmero
Watch what you wish for, it just might come true Smilie
Code:
awk 'NR==FNR{a[NR$2]=$1;next}{print $0,a[FNR$1]}' file2 file1

Hi, thank you so much for your help, but I just tried this and it is not outputting correctly. Smilie
# 6  
Old 09-23-2009
Use GNU awk (gawk), New awk (nawk) or POSIX awk (/usr/xpg4/bin/awk)
# 7  
Old 09-23-2009
Quote:
Originally Posted by danmero
Use GNU awk (gawk), New awk (nawk) or POSIX awk (/usr/xpg4/bin/awk)
Yes, sorry your example works for my example. However, with different IP addresses it doesn't seem to be working. For example:

Code:
File 1:
192.168.1.1 ABC
192.168.1.2 AAA
10.10.1.23 weeee

File 2:
BBB 192.168.1.1
CCC 192.168.1.2
DDD 192.168.1.3
yaaa 10.10.1.23

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

The following output produces:
Code:
192.168.1.1 ABC BBB
192.168.1.2 AAA CCC
10.10.1.23 weeee


Previous Thread | Next Thread
Test Your Knowledge in Computers #723
Difficulty: Medium
Niklaus Emil Wirth, an Austrian computer scientist, designed several programming languages, including Pascal,
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using awk instead of grep -f

Hi Guys. I am trying to count occurances of patterns(occurance can be anywhere in file2) from file1 in file2. file1 is god god pod rod file2 is iamgod iamgod podrod 123rod456 output should be god 2 god 2 pod 1 rod 2 I am not good at awk but i figured out this command.it doesnt... (7 Replies)
Discussion started by: ahfze
7 Replies

2. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

3. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

4. Shell Programming and Scripting

awk / grep

how do I change this line to use the awk command RC19=`grep -c "Broken pipe" $FTP_OUT` Code tags please (5 Replies)
Discussion started by: mbmarciniak
5 Replies

5. Shell Programming and Scripting

AWK/GREP: grep only lines starting with integer

I have an input file 12.4 1.72849432773174e+01 -7.74784188610632e+01 12.5 9.59432114416327e-01 -7.87018212757537e+01 15.6 5.20139995965960e-01 -5.61612429666624e+01 29.3 3.76696387248366e+00 -7.42896194101892e+01 32.1 1.86899877018077e+01 -7.56508762501408e+01 35 6.98857157014640e+00... (2 Replies)
Discussion started by: chrisjorg
2 Replies

6. Shell Programming and Scripting

Read content between xml tags with awk, grep, awk or what ever...

Hello, I trying to extract text that is surrounded by xml-tags. I tried this cat tst.xml | egrep "<SERVER>.*</SERVER>" |sed -e "s/<SERVER>\(.*\)<\/SERVER>/\1/"|tr "|" " " which works perfect, if the start-tag and the end-tag are in the same line, e.g.: <tag1>Hello Linux-Users</tag1> ... (5 Replies)
Discussion started by: Sebi0815
5 Replies

7. Shell Programming and Scripting

Is it better to grep and pipe to awk, or to seach with awk itself

This may just be a lack of experience talking, but I always assumed that when possible it was better to use a commands built in abilities rather than to pipe to a bunch of commands. I wrote a (very simple) script a while back that was meant to pull out a certain error code, and report back what... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

8. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

9. UNIX for Dummies Questions & Answers

grep and awk

I have grep MHz psrinfo-v.out it gives The i386 processor operates at 3000 MHz, The i386 processor operates at 3000 MHz, The i386 processor operates at 3000 MHz, The i386 processor operates at 3000 MHz, how to get instead of these 4 lines: CPU speed: 3000 MHz i.e. CPU... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

10. Shell Programming and Scripting

[grep awk cut] > awk

Hi, I'm very new to scripting. grep $s $filename | awk '{print $2}' | cut -c 1-8 How can I optimize this using a single awk? I tried: awk '/$s/ {print $2}' $filename | cut -c 1-8 However didn't work, I think the awk is not recognizing $s and the verbal is something else. (6 Replies)
Discussion started by: firdousamir
6 Replies

Featured Tech Videos