Help with nawk (Exact Match)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with nawk (Exact Match)
# 1  
Old 12-05-2014
Help with nawk (Exact Match)

I have a file with the contents below "lets say the name of the file is abcxyz" shown at the end of this.

I am using nawk to find the exact ip address and the 6 lines after the match is found using the following nawk statement

Code:
/usr/bin/nawk "/111.46.14.107/,printed==6 { ++printed; print; }" abcxyz

Code:
Result is perfect 

# 111.46.14.107

Line1 Word151 Word152 Word153
Line2 Word154 Word155 Word156
Line3 Word157 Word158 Word159 
Line4 Word160 Word161 Word162 
Line5 Word163 Word164 Word165

But when I try to use "111.46.14.10" ip nawk statement fails as it finds several ips that matches 111.46.14.10 in the file. How to I change the nawk statement to find the exact result


Code:
# 111.46.14.100
# 111.46.14.10
# 111.46.14.101
# 111.46.14.102
# 111.46.14.103
# 111.46.14.104
# 111.46.14.105
# 111.46.14.106
# 111.46.14.107
# 111.46.14.108

The Result that I am looking for is when I use 111.46.14.10 ip in the nawk statement I would like the query to find the following result

Code:
/usr/bin/nawk "/111.46.14.10/,printed==6 { ++printed; print; }" abcxyz

This is what it should return. How do i change the nawk statement to find the exact Match ...  
# 111.46.14.10

Line1 Word46 Word47 Word48
Line2 Word49 Word50 Word51
Line3 Word52 Word53 Word54 
Line4 Word55 Word56 Word57 
Line5 Word58 Word59 Word60



Code:
File name abcxyz

# 111.46.14.98

Line1 Word1 Word2 Word3
Line2 Word4 Word5 Word6
Line3 Word7 Word8 Word9 
Line4 Word10 Word11 Word12 
Line5 Word13 Word14 Word15 

# 111.46.14.99

Line1 Word16 Word17 Word18
Line2 Word19 Word20 Word21
Line3 Word22 Word23 Word24
Line4 Word25 Word26 Word27 
Line5 Word28 Word29 Word30 

# 111.46.14.100

Line1 Word31 Word32 Word33
Line2 Word34 Word35 Word36
Line3 Word37 Word38 Word39 
Line4 Word40 Word41 Word42 
Line5 Word43 Word44 Word45 

# 111.46.14.10

Line1 Word46 Word47 Word48
Line2 Word49 Word50 Word51
Line3 Word52 Word53 Word54 
Line4 Word55 Word56 Word57 
Line5 Word58 Word59 Word60 

# 111.46.14.101

Line1 Word61 Word62 Word63
Line2 Word64 Word65 Word66
Line3 Word67 Word68 Word69 
Line4 Word70 Word71 Word72 
Line5 Word73 Word74 Word75 

# 111.46.14.102

Line1 Word76 Word77 Word78
Line2 Word79 Word80 Word81
Line3 Word82 Word83 Word84 
Line4 Word85 Word86 Word87 
Line5 Word88 Word89 Word90 

# 111.46.14.103

Line1 Word91 Word92 Word93
Line2 Word94 Word95 Word96
Line3 Word97 Word98 Word99 
Line4 Word100 Word101 Word102 
Line5 Word103 Word104 Word105 

# 111.46.14.104

Line1 Word106 Word107 Word108
Line2 Word109 Word110 Word111
Line3 Word112 Word113 Word114 
Line4 Word115 Word116 Word117 
Line5 Word118 Word119 Word120 

# 111.46.14.105

Line1 Word121 Word122 Word123
Line2 Word124 Word125 Word126
Line3 Word127 Word128 Word129 
Line4 Word130 Word131 Word132 
Line5 Word134 Word135 Word136 

# 111.46.14.106

Line1 Word137 Word138 Word139
Line2 Word140 Word141 Word142
Line3 Word143 Word144 Word145 
Line4 Word146 Word147 Word148
Line5 Word149 Word150 Word151 

# 111.46.14.107

Line1 Word151 Word152 Word153
Line2 Word154 Word155 Word156
Line3 Word157 Word158 Word159 
Line4 Word160 Word161 Word162 
Line5 Word163 Word164 Word165 

# 111.46.14.108

Line1 Word166 Word167 Word168
Line2 Word169 Word170 Word171
Line3 Word172 Word173 Word174 
Line4 Word175 Word176 Word177 
Line5 Word178 Word179 Word180

# 2  
Old 12-05-2014
Use /# 111[.]46[.]14[.]10$/

Last edited by Corona688; 12-05-2014 at 12:15 PM..
# 3  
Old 12-05-2014
I get the following error Variable syntax
Code:
/usr/bin/nawk "/# 111[.]46[.]14[.]10$/,printed==5 { ++printed; print; }" abcxyz
Variable syntax

/usr/bin/nawk "/^111.46.14.10$/,printed==5 { ++printed; print; }" abcxyz
Variable syntax
/usr/bin/nawk "/111.46.14.10$/,printed==5 { ++printed; print; }" abcxyz
Variable syntax
/usr/bin/nawk "/111[.]46[.]14[.]10$/,printed==5 { ++printed; print; }" abcxyz
Variable syntax


Last edited by knijjar; 12-05-2014 at 12:27 PM.. Reason: added more examples
# 4  
Old 12-05-2014
use SINGLE quotes....
# 5  
Old 12-05-2014
vgersh99 Thank you So much !!! Works like a butter !!! Smilie

---------- Post updated at 11:55 AM ---------- Previous update was at 11:39 AM ----------

Here is the problem when I use a variable it fails. thats why I had double quotes before. How do I use the nawk statement where I can pass the ip address in a variable !!!

Code:
#!/usr/bin/ksh -xv

# add -xv for debug to above
PATH=/usr/xpg4/bin/:/bin:/usr/bin:/usr/sbin
export PATH
echo "Enter IP Address of unit that you want to be removed :-"
read IP

echo $IP
#This nawk statement will get the IP as soon as it finds  matching IP address from the abcxyz file
/usr/bin/nawk '/$IP$/,printed==6 { ++printed; print; }' abcxyz
 ####################################

---------- Post updated at 12:02 PM ---------- Previous update was at 11:55 AM ----------

This works !!!
Code:
/usr/bin/nawk '/'$IP'$/,printed==6 { ++printed; print; }' abcxyz


Last edited by knijjar; 12-05-2014 at 01:00 PM..
# 6  
Old 12-05-2014
Code:
/usr/bin/nawk -v ip="${IP}" '$0~(ip "$"),printed==6 { ++printed; print; }' abcxyz

# 7  
Old 12-05-2014
Would anyone care to explain the syntax in

Quote:
Originally Posted by vgersh99
Code:
/usr/bin/nawk -v ip="${IP}" '$0~(ip "$"),printed==6 { ++printed; print; }' abcxyz

I don't understand the comma after comparison operator (marked it with red above) Thanks!
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 file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Replacing exact match

Hi All, My Input file contains a 1000’s of lines in which I have to replace a a string to the other. Here the problem is, I have the lines in my Input as below. Cable Yes && !Pay TV && !ADS \noUE \Label="Cable Yes && !Pay TV && !ADS" I want to replace exactly the string Cable Yes &&... (37 Replies)
Discussion started by: am24
37 Replies

3. Shell Programming and Scripting

Get the exact match of the string!

Hi All, I am breaking my head in trying to get a command that will exactly match my given string. I have searched net and found few of the options - grep -F $string file grep -x $string file grep "^${string}$" file awk '/"${string}"/ {print $0}' file strangely nothing seems to... (3 Replies)
Discussion started by: dips_ag
3 Replies

4. UNIX for Dummies Questions & Answers

Exact match question

Hi guys, I am using Centos 6.3. Actually I posted similar question but I still have some minor problem need be fixed. I have two files, file1:target: gi|57529786|ref|NM_001006513.1| mfe: -31.4 kcal/mol p-value: 0.006985 target: gi|403048743|ref|NM_001271159.1| mfe: -29.6 kcal/mol p-value:... (11 Replies)
Discussion started by: yuejian
11 Replies

5. UNIX for Dummies Questions & Answers

Interpolation if there is no exact match for value

Dear all, could you help me with following question. There are two datasets (below). I need to find match between BP values from data1 and data2, and add corresponding CM value from data2 into data1. if there is not exact match, the corresponding CM value should be calculated using interpolation.... (20 Replies)
Discussion started by: kush
20 Replies

6. Shell Programming and Scripting

Exact match using sed

I would like replace all the rows in a file if a row has an exact match to number say 21 in a tab delimited file. I want to delete the row only if it has 21 any of the rows but it should not delecte the row that has 542178 or 563421. I tried this sed '/\<21\>/d' ./inputfile > output.txt ... (7 Replies)
Discussion started by: Kanja
7 Replies

7. Shell Programming and Scripting

Match exact and append zero

file 11 2 12 6 13 7 114 6 011 7 if I'm searching for 11, output needed is output: 11 2 011 7 Code: awk '$1 ~ /^11$/' file I used the above to match exact, but it avoiding "011 7" line too, how to resolve this? (6 Replies)
Discussion started by: Roozo
6 Replies

8. Shell Programming and Scripting

Exact match and #

Hi friends, i am using the following grep command for exact word match: >echo "sachin#tendulkar" | grep -iw "sachin" output: sachin#tendulkar as we can see in the above example that its throwinng the exact match(which is not the case as the keyword is sachin and string is... (6 Replies)
Discussion started by: neelmani
6 Replies

9. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

10. Shell Programming and Scripting

exact match in Perl

Hi By using select clause I'm trying to pull out the rows to a variable. If the variable has 0 row(s) selected then i'm printing some text message else printing some other text message if($xyz =~ m/0 row/) { print "0 rows "; } else { print " There are rows"; } By my problem... (4 Replies)
Discussion started by: pdreddy34
4 Replies
Login or Register to Ask a Question