awk help - matching a field with certail values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk help - matching a field with certail values
# 1  
Old 05-06-2013
HP awk help - matching a field with certail values

Hello there,

I have a file with few fields separated by ":". I wrote a below awk to manipulate this file:

Code:
awk 'BEGIN { FS=OFS=":" }\
NR != 1 && $2 !~ /^98/ && $8 !~ /^6[12][01][0]/{print $0}' $in_file > $out_file

What I wanted was that if $8 field contains any of the values - 6100, 6110, 6200 - it should not be processed. But above pattern will match to 6210 as well and will reject it. What modification should I make in order for pattern not to select 6210 and all 6210s appear in the output?

regards,
juzz4fun

Last edited by vbe; 05-06-2013 at 01:03 PM..
# 2  
Old 05-06-2013
Quote:
Originally Posted by juzz4fun
Hello there,

I have a file with few fields separated by ":". I wrote a below awk to manipulate this file:

awk 'BEGIN { FS=OFS=":" }\
NR != 1 && $2 !~ /^98/ && $8 !~ /^6[12][01][0]/{print $0}' $in_file > $out_file


What I wanted was that if $8 field contains any of the values - 6100, 6110, 6200 - it should not be processed. But above pattern will match to 6210 as well and will reject it. What modification should I make in order for pattern not to select 6210 and all 6210s appear in the output?

regards,
juzz4fun
Code:
awk 'BEGIN { FS=OFS=":" }
NR != 1 && $2 !~ /^98/ && $8 !~ /^6(10|11|20)0/' $in_file > $out_file

will copy lines from $infile to output if and only if:
  1. it is not the 1st line of the file,
  2. the second field in the file does not start with "98", and
  3. the eighth field in the file does not start with 6100, 6110, or 6200.
Note that item #3 in the list says "start with" (which is what your code was doing), but your English description said "contains". If you want "contains" instead of "starts with", change /^6(10|11|20)0/ to /6(10|11|20)0/. If you want "is" instead of "starts with" or "contains", change $8 !~ /^6(10|11|20)0/ to $8 != /6(10|11|20)0/.
# 3  
Old 05-06-2013
Thanks Don Cragun. That helps.
I was looking for line that "starts with" only.. sorry for that... Smilie
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 average field if matching string in another

In the awk below I am trying to get the average of the sum of $7 if the string in $4 matches in the line below it. The --- in the desired out is not needed, it is just to illustrate the calculation. The awk executes and produces the current out. I am not sure why the middle line is skipped and the... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to add text to matching pattern in field

In the awk I am trying to add :p.=? to the end of each $9 that matches the pattern NM_. The below executes andis close but I can not seem to figure out why the :p.=? repeats in the split as in the green in the current output. I have added comments as well. Thank you :). file ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk to print array that occurs the most with matching value in another field

In the below awk I am splitting $7 on the : and then counting each line or NM_xxxx. If the $1 value is the same for each line then print the $7 that occurs the most with the matching $1 value. The awk seems close but I am not sure what is going on. I included a description as well as to what I... (1 Reply)
Discussion started by: cmccabe
1 Replies

5. Shell Programming and Scripting

Sum column values matching other field

this is part of a KT i am going thru. i am writing a script in bash shell, linux where i have 2 columns where 1st signifies the nth hour like 00, 01, 02...23 and 2nd the file size. sample data attached. Desired output is 3 columns which will give the nth hour, number of entries in nth hour and... (3 Replies)
Discussion started by: alpha_1
3 Replies

6. Shell Programming and Scripting

How to print in awk matching $1 values ,to $1,$4 example given.?

Hi Experts, I am trying to get the output from a matching pattern but unable to construct the awk command: file : aa bb cc 11 dd aa cc 33 cc 22 45 68 aa 33 44 44 dd aa cc 37 aa 33 44 67 I want the output to be : ( if $1 match to "aa" start of the line,then print $4 of that line, and... (3 Replies)
Discussion started by: rveri
3 Replies

7. Shell Programming and Scripting

Printing entire field, if at least one row is matching by AWK

Dear all, I have been trying to print an entire field, if the first line of the field is matching. For example, my input looks something like this. aaa ddd zzz 123 987 126 24 0.650 985 354 9864 0.32 0.333 4324 000 I am looking for a pattern,... (5 Replies)
Discussion started by: Chulamakuri
5 Replies

8. Shell Programming and Scripting

AWK : Add Fields of lines with matching field

Dear All, I would like to add values of a field, if the lines match in a certain field. Then I would like to divide the sum though the number of lines that have a matched field. This is the Input: Input: Test1 5 Test1 10 Test2 2 Test2 5 Test2 13 Test3 4 Output: Test1 7.5 Test1 7.5... (6 Replies)
Discussion started by: DerSeb
6 Replies

9. Shell Programming and Scripting

Print matching field using awk

Hi All, I have a string like below: str="Hold=True Map=False 'This will map the data' Run=Yes Modify=False" I want to print the field Run=Yes and retrive the value "Yes". I cannot use simple awk command because the position of the "Run" will be different at different times. Is there a way... (6 Replies)
Discussion started by: deepakgang
6 Replies

10. Shell Programming and Scripting

matching multiple values in awk

How will you change the 5th column in the data file with the value in the second column in the error_correction.txt file. You have to match an extra variable, column 3 of the error_correction file with column 6 of the data.txt file. data.txt: vgr,bugatti veron,,3.5,Maybe,6,.......,ax2,....... (0 Replies)
Discussion started by: VGR
0 Replies
Login or Register to Ask a Question