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
# 8  
Old 05-02-2013
Quote:
Originally Posted by Corona688
Try nawk instead of awk. On some systems, plain awk is very, very old.

nawk not found in AIX
# 9  
Old 05-02-2013
I think there is some confusion. I was merely trying to correct the syntax error in the original post:
Quote:
Originally Posted by arm
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 !!!!
If you check, OP missed a slash and there was a blank space ! ~ instead of !~.

So this has nothing to do with nawk or primitive awk
This User Gave Thanks to Yoda For This Post:
# 10  
Old 05-02-2013
Lightbulb

Quote:
Originally Posted by arm
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 :
...
Maybe simply type this in?
Code:
awk '($1=="10" || $1=="20") && ($2=="10" || $2=="20") && $3>"130302" {print}' myfile

Or modify it a bit like this?
Code:
awk '($1=="10" || $1=="20") && ($2=="10" || $2=="20") && $3>="130303" {print}' myfile


Last edited by MadeInGermany; 05-02-2013 at 07:24 PM.. Reason: bulb icon
This User Gave Thanks to MadeInGermany For This Post:
# 11  
Old 05-02-2013
Quote:
can you explain substr ($3, 0, 6) > 130302
Sure, substr is a function that returns a "substring".

$3 - obviously, that's the field to work on.

0 - starting position. Properly, should have been 1, since awk numbers starting from 1 for this particular thing. It does not mind 0, but 1 is the correct lowest starting position.

6 - length of substring to return.

-------------------

! ~ normally needs to be together like !~to work correctly. My gnu awk requires that, anyway.

Hope this helps. Smilie
This User Gave Thanks to hanson44 For This Post:
# 12  
Old 05-06-2013
but how to do that ? I want to Filter out any record dose belong to “263” in $4 and $6 is before 130304 ?

Code:
2    9647701612350         9647701168456         262       23        1303031257462B0300 1303031259182B0300 92        9647701146402  0
5    9647706046060         9647801139306         263       32        1303031255312B0300 1303031259182B0300 227       9647701146402  0
6    9647706046060         9647801139306         263       32        130325255312B0300 1303251259182B0300 227       9647701146402  0
8    9647701612350         9647701168456         22       262        1303111257462B0300 1303111259182B0300 92        9647701146402  0
9    9647701612350         9647701168456         262      700        1303131257462B0300 1303131259182B0300 92        9647701146402  0
10   9647701612350         9647701168456         22       263        1303031257462B0300 1303031259182B0300 92        9647701146402  0
12   9647706046060         9647801139306         263       32        130303255312B0300 1303032259182B0300 227       9647701146402  0

the output must be

Code:
2    9647701612350         9647701168456         262       23        1303031257462B0300 1303031259182B0300 92        9647701146402  0
6    9647706046060         9647801139306         263       32        130325255312B0300 1303251259182B0300 227       9647701146402  0
8    9647701612350         9647701168456         22       262        1303111257462B0300 1303111259182B0300 92        9647701146402  0
9    9647701612350         9647701168456         262      700        1303131257462B0300 1303131259182B0300 92        9647701146402  0
10   9647701612350         9647701168456         22       263        1303031257462B0300 1303031259182B0300 92        9647701146402  0


Last edited by Scott; 05-06-2013 at 09:05 PM.. Reason: Code tags
# 13  
Old 05-06-2013
Quote:
Filter out any record dose belong to “263” in $4 and $6 is before 130304
Here are two equivalent ways:

Code:
$ awk '$4 == 263 && substr ($6, 1, 6) < 130304 { next } { print }' input
2    9647701612350         9647701168456         262       23        1303031257462B0300 1303031259182B0300 92        9647701146402  0
6    9647706046060         9647801139306         263       32        130325255312B0300 1303251259182B0300 227       9647701146402  0
8    9647701612350         9647701168456         22       262        1303111257462B0300 1303111259182B0300 92        9647701146402  0
9    9647701612350         9647701168456         262      700        1303131257462B0300 1303131259182B0300 92        9647701146402  0
10   9647701612350         9647701168456         22       263        1303031257462B0300 1303031259182B0300 92        9647701146402  0

Code:
$ awk '$4 != 263 || substr ($6, 1, 6) >= 130304 { print }' input
2    9647701612350         9647701168456         262       23        1303031257462B0300 1303031259182B0300 92        9647701146402  0
6    9647706046060         9647801139306         263       32        130325255312B0300 1303251259182B0300 227       9647701146402  0
8    9647701612350         9647701168456         22       262        1303111257462B0300 1303111259182B0300 92        9647701146402  0
9    9647701612350         9647701168456         262      700        1303131257462B0300 1303131259182B0300 92        9647701146402  0
10   9647701612350         9647701168456         22       263        1303031257462B0300 1303031259182B0300 92        9647701146402  0

# 14  
Old 05-08-2013
Quote:
Originally Posted by hanson44
Here are two equivalent ways:

Code:
$ awk '$4 == 263 && substr ($6, 1, 6) < 130304 { next } { print }' input
2    9647701612350         9647701168456         262       23        1303031257462B0300 1303031259182B0300 92        9647701146402  0
6    9647706046060         9647801139306         263       32        130325255312B0300 1303251259182B0300 227       9647701146402  0
8    9647701612350         9647701168456         22       262        1303111257462B0300 1303111259182B0300 92        9647701146402  0
9    9647701612350         9647701168456         262      700        1303131257462B0300 1303131259182B0300 92        9647701146402  0
10   9647701612350         9647701168456         22       263        1303031257462B0300 1303031259182B0300 92        9647701146402  0

Code:
$ awk '$4 != 263 || substr ($6, 1, 6) >= 130304 { print }' input
2    9647701612350         9647701168456         262       23        1303031257462B0300 1303031259182B0300 92        9647701146402  0
6    9647706046060         9647801139306         263       32        130325255312B0300 1303251259182B0300 227       9647701146402  0
8    9647701612350         9647701168456         22       262        1303111257462B0300 1303111259182B0300 92        9647701146402  0
9    9647701612350         9647701168456         262      700        1303131257462B0300 1303131259182B0300 92        9647701146402  0
10   9647701612350         9647701168456         22       263        1303031257462B0300 1303031259182B0300 92        9647701146402  0

thanks I manage to solve it using below code , you may want to take a look and tell me whether correct or not ?

Code:
awk !'($4 ~/263/  && $6 <=130304) {print }'  input

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