how do i pattern match a field with awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how do i pattern match a field with awk?
# 1  
Old 06-03-2008
how do i pattern match a field with awk?

hi,

let's say $numbers = "324 350 587" an so on...
what i'm trying to do is this:

awk -v numbers="$numbers" '{if (numbers ~ /$2/) print $0, "bla bla"}' file

# file looks like this:
214 .....
215 ...
216 ....
250
...
324
325
...
350 something ...
...
587 ...

i want to do something with the lines that contain the numbers in the variable numbers, and something else with the lines that match other patterns.

thanks..
# 2  
Old 06-03-2008
not sure what you're after....
Is it: if the line contains one of the numbers, then 'blah blah'?
# 3  
Old 06-03-2008
yep

forgot to mention - the line i wrote didn't work i guess it has to do with the syntax

and yes, what i meant to do was:
if the line contains one of the numbers (in the 2ed field), then print $0, "..something...."
# 4  
Old 06-03-2008
nawk -f some.awk myFile
OR
nawk -v numbers='324 350 587' -f some.awk myFile

some.awk:
Code:
BEGIN {
  if (numbers =="") numbers="12 13"
  gsub("[0-9]+", "(^&$)", numbers)
  gsub(" ", "|", numbers)
}

$2 ~ numbers { print $0, "blah blah" }

# 5  
Old 06-03-2008
answer

omg, isn't there a simple way to do this?

how can i make awk recognize the second field as a pattern?

this doesn't work:
awk -v numbers="$numbers" '{if (numbers ~ /$2/) print $0, "bla bla"}' file

but this works (when i put actual number):
awk -v numbers="$numbers" '{if (numbers ~ /345/) print $0, "bla bla"}' file

how should i write it so that awk will "see" the actual number that is in the file (in the 2ed field)??

thanks so much for trying to help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get output of multiple pattern match from first field to a file

Hi All, Greetings! I have a file of 40000+ lines with different entries, I need matching entries filterd out to their files based on first filed pattern for the matching : For example: All server1 entries (in field1) to come together with its path in 2nd field. The best output I want... (9 Replies)
Discussion started by: rveri
9 Replies

2. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk to match value to a field within +/- value

In the awk below I use $2 of filet to search filea for a match. If the values in $2 are exact match this works great. However, that is not always the case, so I need to perform the search using a range of + or - 2. That is if the value in filea $2 is within + or - 2 of filet $2 then it is matched.... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

awk Match First Field and Replace Second Column

Hi Friends, I have looked around the forums and over online but couldn't figure out how to deal with this problem input.txt gene1,axis1/0/1,axis2/0/1 gene1,axis1/1/2,axis2/1/2 gene1,axis1/2/3,axis2/2/3 gene2,axis1/3/4,axis2/3/4 Match on first column and if first column is... (1 Reply)
Discussion started by: jacobs.smith
1 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. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

7. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

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

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. UNIX for Dummies Questions & Answers

Awk counting lines with field match

Hi, Im trying to create a script that reads throught every line in a file and then counts how many lines there with a certain field that matches a input, and also ausing another awk it has to do the same as the above but to then use sort anduniq to get rid of all the unique lines with another... (8 Replies)
Discussion started by: fredted40x
8 Replies
Login or Register to Ask a Question