awk to print record not equal specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print record not equal specific pattern
# 1  
Old 05-02-2013
Lightbulb awk & get any line with ( specific charactors from pattern )

how to use "awk" to print any record has pattern not equal ? for example my file has 5 records & I need to get all lines which $1=10 or 20 , $2=10 or 20 and $3 greater than "130302" as it shown :

Code:
10  20  1303252348212B030 
20  10  1303242348212B030 
40  34  1303252348212B030 
10  20  1303032348212B030 
10  20  1303022348212B030

I have tried below

Code:
awk '( $1 ~/[12]0/  ||  $2 ~/[12]0 ) && $3 ! ~/130302/ { print $0 }'  my file

I think awk is confused with the digits after 130302XXXXXX !!!!

Last edited by Scott; 05-06-2013 at 09:04 PM.. Reason: More code tags
# 2  
Old 05-02-2013
First of all I didn't understand your requirement clearly.

But I noticed few syntax error in your code:
Code:
awk '( $1 ~ /[12]0/  ||  $2 ~ /[12]0/ ) && $3 !~ /130302/ { print $0 }' myfile

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-02-2013
dear as far as I know nothing wrong with backslash / and not equal ! =
my request is to get following output :
Code:
10  20  1303252348212B030 
20  10  1303242348212B030 
10  20  1303032348212B030

from below input
Code:
10  20  1303252348212B030 
20  10  1303242348212B030 
40  34  1303252348212B030 
10  20  1303032348212B030 
10  20  1303022348212B030


Last edited by Franklin52; 05-03-2013 at 03:15 AM.. Reason: Code tags
# 4  
Old 05-02-2013
Code:
$ awk '($1 ~ /[12]0/ || $2 ~ /[12]0/) && substr ($3, 0, 6) > 130302 { print }' input
10 20 1303252348212B030
20 10 1303242348212B030
10 20 1303032348212B030

# 5  
Old 05-02-2013
Quote:
Originally Posted by arm
dear as far as I know nothing wrong with backslash / and not equal ! =
Ok so there is nothing wrong with backslash:
Code:
awk '$2 ~ /[12]0' myfile
 syntax error The source line is 1.
 The error context is
                $2 ~ >>>  /[12]0 <<<

And there is nothing wrong with regexp not matching:
Code:
awk '$2 ! ~/[12]0' myfile
 syntax error The source line is 1.
 The error context is
                $2 ! >>>  ~ <<<

I hope then you have some explanation for above behavior?
This User Gave Thanks to Yoda For This Post:
# 6  
Old 05-02-2013
Quote:
Originally Posted by hanson44
Code:
$ awk '($1 ~ /[12]0/ || $2 ~ /[12]0/) && substr ($3, 0, 6) > 130302 { print }' input
10 20 1303252348212B030
20 10 1303242348212B030
10 20 1303032348212B030

can you explain
Code:
substr ($3, 0, 6) > 130302

---------- Post updated at 05:04 PM ---------- Previous update was at 05:02 PM ----------

Quote:
Originally Posted by Yoda
Ok so there is nothing wrong with backslash:
Code:
awk '$2 ~ /[12]0' myfile
 syntax error The source line is 1.
 The error context is
                $2 ~ >>>  /[12]0 <<<

And there is nothing wrong with regexp not matching:
Code:
awk '$2 ! ~/[12]0' myfile
 syntax error The source line is 1.
 The error context is
                $2 ! >>>  ~ <<<

I hope then you have some explanation for above behavior?
do this
Code:
awk '$2 ~ /[12]0/' myfile

and this
Code:
awk '$2 ! ~/[12]0/' myfile

you still have error message ?
# 7  
Old 05-02-2013
Quote:
Originally Posted by Yoda
Ok so there is nothing wrong with backslash:
Code:
awk '$2 ~ /[12]0' myfile
 syntax error The source line is 1.
 The error context is
                $2 ~ >>>  /[12]0 <<<

And there is nothing wrong with regexp not matching:
Code:
awk '$2 ! ~/[12]0' myfile
 syntax error The source line is 1.
 The error context is
                $2 ! >>>  ~ <<<

I hope then you have some explanation for above behavior?
Try nawk instead of awk. On some systems, plain awk is very, very old.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find key pattern and print selected lines for each record

Hi, I need help on a complicated file that I am working on. I wanted to extract important info from a very huge file. It is space delimited file. I have hundred thousands of records in this file. An example content of the inputfile as below:- ## ID Ser402 Old; 23... (2 Replies)
Discussion started by: redse171
2 Replies

2. Shell Programming and Scripting

Help with print out record if first and next line follow specific pattern

Input file: pattern1 100 250 US pattern2 50 3050 UK pattern3 100 250 US pattern1 70 1050 UK pattern1 170 450 Mal pattern2 40 750 UK . . Desired Output file: pattern1 100 250 US pattern2 50 3050 UK pattern1 170 450 Mal pattern2... (3 Replies)
Discussion started by: cpp_beginner
3 Replies

3. Shell Programming and Scripting

Problem to print out record got smallest number in specific column

Hi, Anybody know how to print out the record that shown smallest number among column 3 and column 4 Case 1 Input : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Case 1 Output : 37170 37196 77 51 Case 2 Input : 469613 469660 73 ... (4 Replies)
Discussion started by: cpp_beginner
4 Replies

4. Shell Programming and Scripting

Execution problem with print out record that follow specific pattern

Hi, Do anybody know how to print out only those record that column 1 is "a" , then followed by "b"? Input file : a comp92 2404242 2405172 b comp92 2405303 2406323 b comp92 2408786 2410278 a comp92 2410271 2410337 a comp87 1239833 1240418 b comp87... (3 Replies)
Discussion started by: patrick87
3 Replies

5. Shell Programming and Scripting

Help with print out line that have different record in specific column

Input file 1: - 7367 8198 - 8225 9383 + 9570 10353 Input file 2: - 2917 3667 - 3851 4250 + 4517 6302 + 6302 6740 + 6768 7524 + 7648 8170 + 8272 8896 + 8908 9915 - 10010 ... (18 Replies)
Discussion started by: perl_beginner
18 Replies

6. UNIX for Dummies Questions & Answers

How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me. I have this input: the great warrior a, b, c and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this : Warrior Type a b c Im still very... (3 Replies)
Discussion started by: radynaraya
3 Replies

7. Shell Programming and Scripting

Help with print out all relevant record if match particular pattern

Input file: data100_content1 420 700 data101_content1 107 516 data101_content2 194 773 data101_content3 195 917 data104_content2 36 325 data105_content1 505 605 data106_content1 291 565 ... (7 Replies)
Discussion started by: perl_beginner
7 Replies

8. Shell Programming and Scripting

Using awk, print all the lines where field 8 is equal to x

Using awk, print all the lines where field 8 is equal to x I really did try, but this awk thing is really hard to figure out. file1.txt"Georgia","Atlanta","2011-11-02","x","","","","" "California","Los Angeles","2011-11-03","x","","","",""... (2 Replies)
Discussion started by: charles33
2 Replies

9. Shell Programming and Scripting

Regarding multiline record searching with specific pattern

Dear Experts, I need to extract specific records from one file which has multiline records. Input file pattern is: ============ aaaaaaaa bbbbbbbb asdf 1234 cccccccc dddddddd ============ aaaaaaaa bbbbbbbb qwer 2345 cccccccc dddddddd (7 Replies)
Discussion started by: dhiraj4mann
7 Replies

10. Shell Programming and Scripting

Grep for a pattern and print entire record

Hi friends, This is my very first post on forum, so kindly excuse if my doubts are found too silly. I am trying to automate a piece of routine work and this is where I am stuck at the moment-I need to grep a particular ID through a file containing many records(which start with <LRECORD> and end... (6 Replies)
Discussion started by: faiz1985
6 Replies
Login or Register to Ask a Question