Match exact Variable in Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match exact Variable in Awk
# 1  
Old 06-27-2012
Match exact Variable in Awk

I want to match exact string stored in Variable INO and print next two lines. I have written below code which partially matches:

Code:
awk '/\'$VAR'/{getline; getline; print}'

Will be thankful , if somebody can help in to resolve this problem.



Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 06-27-2012 at 01:26 PM.. Reason: code tags
# 2  
Old 06-28-2012
Like this?

Code:
awk '$0 == "'"$var"'" {getline;print;getline;print}' inputfile

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 06-28-2012
Thanks Elixir, but that too not working Smilie
# 4  
Old 06-28-2012
What is "not working"?

Please post a representative sample of input and the expected output.
# 5  
Old 06-28-2012
What about:
Code:
~/unix.com$ awk -v var='text2' '$0==var{getline x;getline;print x RS $0}' file

set var to search what you want for
Code:
~/unix.com$ cat file
text1
text2
text3
text4
text5
~/unix.com$ awk -v var='text2' '$0==var{getline x;getline;print x RS $0}' file
text3
text4

# 6  
Old 06-28-2012
If it supports -A, just use grep. It'll also handle the case where the pattern matches within two lines of the previous match (if that's a possible scenario, the AWK solutions above will not handle it correctly).

Regards,
Alister
# 7  
Old 06-28-2012
Hi,

Here is the sample example:


Code:
var=$(echo $i | cut -s -d. -f1 )

Where i contains a:2.b:3

I have an output as follows :

Code:
  b:2 a:2
  a
  b
  b:22 a:22
  c
  d

I want to print only b which is at next to next line of pattern a:2.

I am using
Code:
awk '/\'$var'/{getline; getline; print}'

but it prints b and d both, as it matches a:2 and a:22 .

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 06-28-2012 at 03:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Vlookup using awk without exact match

Code used to find the server from cloum 3 and update needtotakesnap Output came from above command awk 'NR==FNR{A;next}$3 in A{$3 = "needtotakesnap " $3}1' /home/Others/active-server.txt /home/Others/all-server |grep server1 879 dummy server1_217_silver dummy 00870 TDEV 2071575 831 Tier1... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

2. Shell Programming and Scripting

Vlookup using awk without exact match for two colum input

Source file 1 335 R1-snapfound 0098F RDFType:R1 R2-Dev R2-snapfound ,010C0 RemoteSymmetrixID:345 335 R1-snapfound 00990 RDFType:R1 R2-Dev R2-snapfound ,010C1 RemoteSymmetrixID:345 335 R1-snapfound 009C0 RDFType:R1 R2-Dev R2-snapfound ,009C1 RemoteSymmetrixID:345 335 R1-snapfound 009C1... (5 Replies)
Discussion started by: ranjancom2000
5 Replies

3. 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

4. Shell Programming and Scripting

awk match shell variable that contains special characters?

How to match a shell variable that contains parenthesis (and other special characters like "!") file.txt contains: Charles Dickens Matthew Lewis (writer) name="Matthew Lewis (writer)"; awk -v na="$name" ' $0 ~ na' file.txt Ideally this would match $name in file.txt (in this... (3 Replies)
Discussion started by: Mid Ocean
3 Replies

5. 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

6. Shell Programming and Scripting

awk match whole word using a variable

Hello, I am writing a script in awk where I need to match a whole word that is stored inside a variable. For example: I am working on a text that looks like this, and I want to print the second row: sfasfsomethingsfasf this is something I can use this is not somethingIcanuse ... (12 Replies)
Discussion started by: avi.levi
12 Replies

7. 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

8. 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

9. Shell Programming and Scripting

How to match a exact word in a variable ????

Hi All, Str="online maintenance" if then perform some action I know the above comparison is wrong ..... Actually I am very new to this solaris.. I want to check for online in a variable and perform some action based on that How can I do it? (6 Replies)
Discussion started by: vijaysachin
6 Replies

10. UNIX for Advanced & Expert Users

can awk built-in "match" be exact??

hello everybody, as explained in the title, here is what I want: str1="name1 name2 name3" str2="name1" str3="name" I know that match(str1,str2) will return 1, but I want that match(str1,str3) returns 0 (when it also returns 1...) Is there a way to get that exact matching process done... (6 Replies)
Discussion started by: farphe
6 Replies
Login or Register to Ask a Question