awk expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk expressions
# 1  
Old 10-01-2014
Question awk expressions

when I use
Code:
Input command | awk '$2~/[0-3]:[3]:[0-3]/'
300 12:3:0  FC    15 normal    559104    525312 6:6:1* 7:6:1           600
301 12:3:1  FC    15 normal    559104    525312 6:6:1  7:6:1*          600
302 12:3:2  FC    15 normal    559104    524288 6:6:1* 7:6:1           600
303 12:3:3  FC    15 normal    559104    524288 6:6:1  7:6:1*          600
324 13:3:0  FC    15 normal    559104    525312 6:6:2* 7:6:2           600
325 13:3:1  FC    15 normal    559104    525312 6:6:2  7:6:2*          600
326 13:3:2  FC    15 normal    559104    524288 6:6:2* 7:6:2           600
327 13:3:3  FC    15 normal    559104    524288 6:6:2  7:6:2*          600
408 10:3:0  FC    15 normal    559104    224256 4:6:3* 5:6:3           600
409 10:3:1  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
410 10:3:2  FC    15 normal    559104    225280 4:6:3* 5:6:3           600
411 10:3:3  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
420 11:3:0  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
421 11:3:1  FC    15 normal    559104    224256 4:6:4  5:6:4*          600
422 11:3:2  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
423 11:3:3  FC    15 normal    559104    225280 4:6:4  5:6:4*          600

Here I don't want (12,13,10,11), and if I write
Code:
awk '$2~/[0-12]:[3]:[0-3]/'

where I want first bit to be 0 all the way to 12, I don't get correct results.

Output
Code:
input command| awk '$2~/[0-12]:[3]:[0-3]/'
300 12:3:0  FC    15 normal    559104    525312 6:6:1* 7:6:1           600
301 12:3:1  FC    15 normal    559104    525312 6:6:1  7:6:1*          600
302 12:3:2  FC    15 normal    559104    524288 6:6:1* 7:6:1           600
303 12:3:3  FC    15 normal    559104    524288 6:6:1  7:6:1*          600
408 10:3:0  FC    15 normal    559104    224256 4:6:3* 5:6:3           600
409 10:3:1  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
410 10:3:2  FC    15 normal    559104    225280 4:6:3* 5:6:3           600
411 10:3:3  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
420 11:3:0  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
421 11:3:1  FC    15 normal    559104    224256 4:6:4  5:6:4*          600
422 11:3:2  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
423 11:3:3  FC    15 normal    559104    225280 4:6:4  5:6:4*          600


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 10-01-2014 at 11:52 AM.. Reason: code tags...
# 2  
Old 10-01-2014
Code:
awk '$2~/^(1[0-2]|[0-9]):3:[0-3]/'

# 3  
Old 10-01-2014
Quote:
Originally Posted by vishalgoyal
when I use
Code:
Input command | awk '$2~/[0-3]:[3]:[0-3]/'
300 12:3:0  FC    15 normal    559104    525312 6:6:1* 7:6:1           600
301 12:3:1  FC    15 normal    559104    525312 6:6:1  7:6:1*          600
302 12:3:2  FC    15 normal    559104    524288 6:6:1* 7:6:1           600
303 12:3:3  FC    15 normal    559104    524288 6:6:1  7:6:1*          600
324 13:3:0  FC    15 normal    559104    525312 6:6:2* 7:6:2           600
325 13:3:1  FC    15 normal    559104    525312 6:6:2  7:6:2*          600
326 13:3:2  FC    15 normal    559104    524288 6:6:2* 7:6:2           600
327 13:3:3  FC    15 normal    559104    524288 6:6:2  7:6:2*          600
408 10:3:0  FC    15 normal    559104    224256 4:6:3* 5:6:3           600
409 10:3:1  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
410 10:3:2  FC    15 normal    559104    225280 4:6:3* 5:6:3           600
411 10:3:3  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
420 11:3:0  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
421 11:3:1  FC    15 normal    559104    224256 4:6:4  5:6:4*          600
422 11:3:2  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
423 11:3:3  FC    15 normal    559104    225280 4:6:4  5:6:4*          600

Here I don't want (12,13,10,11), and if I write
Code:
awk '$2~/[0-12]:[3]:[0-3]/'

where I want first bit to be 0 all the way to 12, I don't get correct results.

Output
Code:
input command| awk '$2~/[0-12]:[3]:[0-3]/'
300 12:3:0  FC    15 normal    559104    525312 6:6:1* 7:6:1           600
301 12:3:1  FC    15 normal    559104    525312 6:6:1  7:6:1*          600
302 12:3:2  FC    15 normal    559104    524288 6:6:1* 7:6:1           600
303 12:3:3  FC    15 normal    559104    524288 6:6:1  7:6:1*          600
408 10:3:0  FC    15 normal    559104    224256 4:6:3* 5:6:3           600
409 10:3:1  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
410 10:3:2  FC    15 normal    559104    225280 4:6:3* 5:6:3           600
411 10:3:3  FC    15 normal    559104    225280 4:6:3  5:6:3*          600
420 11:3:0  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
421 11:3:1  FC    15 normal    559104    224256 4:6:4  5:6:4*          600
422 11:3:2  FC    15 normal    559104    225280 4:6:4* 5:6:4           600
423 11:3:3  FC    15 normal    559104    225280 4:6:4  5:6:4*          600


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks
Hello vishalgoyal,

Following may help you, Lets say our input file is as follows( I have added few testing lines in it for testing purpose, If I have understood request properly.)
Input_file:
Code:
cat file123
300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
300 12:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
301 12:3:1 FC 15 normal 559104 525312 6:6:1 7:6:1* 600
302 12:3:2 FC 15 normal 559104 524288 6:6:1* 7:6:1 600
303 12:3:3 FC 15 normal 559104 524288 6:6:1 7:6:1* 600
324 13:3:0 FC 15 normal 559104 525312 6:6:2* 7:6:2 600
325 13:3:1 FC 15 normal 559104 525312 6:6:2 7:6:2* 600
326 13:3:2 FC 15 normal 559104 524288 6:6:2* 7:6:2 600
327 13:3:3 FC 15 normal 559104 524288 6:6:2 7:6:2* 600
408 10:3:0 FC 15 normal 559104 224256 4:6:3* 5:6:3 600
409 10:3:1 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
410 10:3:2 FC 15 normal 559104 225280 4:6:3* 5:6:3 600
411 10:3:3 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
420 11:3:0 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
421 11:3:1 FC 15 normal 559104 224256 4:6:4 5:6:4* 600
422 11:3:2 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
423 11:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
455 04:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
344 06:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600

Code is as follows:
Code:
awk '{if($2 ~ /[0-1][0-9]:[3]:[0-3]/){print $0}}' file123

Output will be as follows.
Code:
300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
300 12:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
301 12:3:1 FC 15 normal 559104 525312 6:6:1 7:6:1* 600
302 12:3:2 FC 15 normal 559104 524288 6:6:1* 7:6:1 600
303 12:3:3 FC 15 normal 559104 524288 6:6:1 7:6:1* 600
324 13:3:0 FC 15 normal 559104 525312 6:6:2* 7:6:2 600
325 13:3:1 FC 15 normal 559104 525312 6:6:2 7:6:2* 600
326 13:3:2 FC 15 normal 559104 524288 6:6:2* 7:6:2 600
327 13:3:3 FC 15 normal 559104 524288 6:6:2 7:6:2* 600
408 10:3:0 FC 15 normal 559104 224256 4:6:3* 5:6:3 600
409 10:3:1 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
410 10:3:2 FC 15 normal 559104 225280 4:6:3* 5:6:3 600
411 10:3:3 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
420 11:3:0 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
421 11:3:1 FC 15 normal 559104 224256 4:6:4 5:6:4* 600
422 11:3:2 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
423 11:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
455 04:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
344 06:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600

As we can see in 2nd field's first 2 numbers it is catching the digits now from 00-12, please let me know you have any queries.

EDIT: Also to get 2nd field's first 2 digits as 0-3 following can help.

Code:
awk '{if($2 ~ /[0][0-3]:[3]:[0-3]/){print $0}}' file123

Output will be as follows.

Code:
300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-01-2014 at 12:33 PM.. Reason: Added 2nd problem's solution too+removed i
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 10-01-2014
Post a sample of the input and output...
# 5  
Old 10-01-2014
try it prints line if hour is in between 00-09 or 14-23

Code:
awk  '$2 ~ /(0[0-9]|1[4-9]|2[0-3]):3:[0-9]/' file


Last edited by Akshay Hegde; 10-01-2014 at 12:34 PM.. Reason: fix typo
# 6  
Old 10-01-2014
Maybe,

^(1[0-2]|0[0-9]):3:[0-3]
# 7  
Old 10-01-2014
Quote:
Originally Posted by RavinderSingh13
Hello vishalgoyal,

Following may help you, Lets say our input file is as follows( I have added few testing lines in it for testing purpose, If I have understood request properly.)
Input_file:
Code:
cat file123
300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
300 12:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
301 12:3:1 FC 15 normal 559104 525312 6:6:1 7:6:1* 600
302 12:3:2 FC 15 normal 559104 524288 6:6:1* 7:6:1 600
303 12:3:3 FC 15 normal 559104 524288 6:6:1 7:6:1* 600
324 13:3:0 FC 15 normal 559104 525312 6:6:2* 7:6:2 600
325 13:3:1 FC 15 normal 559104 525312 6:6:2 7:6:2* 600
326 13:3:2 FC 15 normal 559104 524288 6:6:2* 7:6:2 600
327 13:3:3 FC 15 normal 559104 524288 6:6:2 7:6:2* 600
408 10:3:0 FC 15 normal 559104 224256 4:6:3* 5:6:3 600
409 10:3:1 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
410 10:3:2 FC 15 normal 559104 225280 4:6:3* 5:6:3 600
411 10:3:3 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
420 11:3:0 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
421 11:3:1 FC 15 normal 559104 224256 4:6:4 5:6:4* 600
422 11:3:2 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
423 11:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
455 04:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
344 06:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600

Code is as follows:
Code:
awk '{i=0;if($2 ~ /[0-1][0-9]:[3]:[0-3]/){print $0;i++}}' file123

Output will be as follows.
Code:
300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
300 12:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
301 12:3:1 FC 15 normal 559104 525312 6:6:1 7:6:1* 600
302 12:3:2 FC 15 normal 559104 524288 6:6:1* 7:6:1 600
303 12:3:3 FC 15 normal 559104 524288 6:6:1 7:6:1* 600
324 13:3:0 FC 15 normal 559104 525312 6:6:2* 7:6:2 600
325 13:3:1 FC 15 normal 559104 525312 6:6:2 7:6:2* 600
326 13:3:2 FC 15 normal 559104 524288 6:6:2* 7:6:2 600
327 13:3:3 FC 15 normal 559104 524288 6:6:2 7:6:2* 600
408 10:3:0 FC 15 normal 559104 224256 4:6:3* 5:6:3 600
409 10:3:1 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
410 10:3:2 FC 15 normal 559104 225280 4:6:3* 5:6:3 600
411 10:3:3 FC 15 normal 559104 225280 4:6:3 5:6:3* 600
420 11:3:0 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
421 11:3:1 FC 15 normal 559104 224256 4:6:4 5:6:4* 600
422 11:3:2 FC 15 normal 559104 225280 4:6:4* 5:6:4 600
423 11:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
455 04:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600
344 06:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600

As we can see in 2nd field's first 2 numbers it is catching the digits now from 00-12, please let me know you have any queries.

EDIT: Also to get 2nd field's first 2 digits as 0-3 following can help.

Code:
awk '{i=0;if($2 ~ /[0][0-3]:[3]:[0-3]/){print $0;i++}}' file123

Output will be as follows.

Code:
300 02:3:0 FC 15 normal 559104 525312 6:6:1* 7:6:1 600
433 00:3:3 FC 15 normal 559104 225280 4:6:4 5:6:4* 600

Thanks,
R. Singh
Hi R.Singh!

What is the purpose of using variable i and incrementing it ? I think thread poster not asked for count am I right ? even if suppose had a need to count matches then still it fails since variable i will reset as soon as awk reads next line. Hope you don't mind my comments / compliments
This User Gave Thanks to Akshay Hegde 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

awk or sed or python for regular expressions ?

Linux 6.X environments (RHEL, Oracle Linux ) I could write basic shell scripts in bash. In my spare time, I was planning to learn awk or sed to deal with regular expression tasks I have to deal with. But, I gather that python is gaining popularity these days and I came to know that python has a... (5 Replies)
Discussion started by: John K
5 Replies

2. Shell Programming and Scripting

Assistance required with awk and regular expressions

Hello there, I am trying to get my head around the section below of a script we use that incorporates AWK and Regular Expressions. { match($0,"The broker*");print $1,$2,$3 ":", substr($0, RSTART,RLENGTH)} I have a basic understanding of how match works, what I am struggling with is the... (2 Replies)
Discussion started by: jimbojames
2 Replies

3. Shell Programming and Scripting

Problems with reg.-expressions in a awk-search-pattern

Hi, i have some problems with regular expressions in a awk search pattern. what i want to do: i want to calculate the mean-value in the time from 00:00 to 06:00 how my data looks like: .... 04/01/13-01:40 670992 54802 80711 116460 156177 04/01/13-01:50 703725 60150 85498 ... (3 Replies)
Discussion started by: IMPe
3 Replies

4. UNIX for Advanced & Expert Users

Awk expressions working & not working

Hi, Putting across a few awk expressions. Apart from the last, all of them are working. echo a/b/c | awk -F'/b/c$' '{print $1}' a echo a/b/c++ | awk -F'/b/c++' '{print $1}' a echo a/b/c++ | awk -F'/b/c++$' '{print $1}' a/b/c++ Request thoughts on why putting a '$' post double ++... (12 Replies)
Discussion started by: vibhor_agarwali
12 Replies

5. Shell Programming and Scripting

Awk- How to extract duplicate expressions

How to extract duplicate expressions ? CD of c4 input c3 100 120 TF03_X2 + AABDDAAABDDBCBACDBBC c4 100 120 TF03_X3 + AABCDAAABDDBCBACDBBC Script awk '{ for(i=1; i<=NF; i++) if($5 == "+" && $6 ~/CD/) {print index($6,... (11 Replies)
Discussion started by: bumblebee_2010
11 Replies

6. Shell Programming and Scripting

Test Regular Expressions on Arrays in Awk

How would I test for a suffix on an element in an array? e.g. testing for /$html/ of an element array (4 Replies)
Discussion started by: ROFL
4 Replies

7. Shell Programming and Scripting

Handling regular expressions in awk

Script is: accept filename as argument(also handle CTRL+C).to check whether th file exist in the current directory,it it then using awk find the employees who are either born in 1990 or drawing a salary greater than 25000. In my database file 1st field is of id ,2nd field is name,5th field is of... (5 Replies)
Discussion started by: Priyanka Bhati
5 Replies

8. Shell Programming and Scripting

Regular Expressions

#!/usr/bin/perl $word = "one last challenge"; if ( $word =~ /^(\w+).*\s(\w+)$/ ) { print "$1"; print "\n"; print "$2"; } The output shows that "$1" is with result one and "$2" is with result challenge. I am confused about how this pattern match expression works step by step. I... (8 Replies)
Discussion started by: DavidHe
8 Replies

9. Shell Programming and Scripting

Awk regular expressions

Hi Experts, Can you please help me out for the below scenario, I have a variable length file with the fixed number of columns, in which the fields are delimited by pipe symbol (|). From that file I have to extract the lines which has the following scenario, The field1 in a... (1 Reply)
Discussion started by: chella
1 Replies

10. Shell Programming and Scripting

printing between two expressions using AWK

Hi, I am trying to print lines between two expressions using AWK in the following manner eg ABB 10 10 20 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 11 11 ACC 10 10 10 10 awk '/ABB/,/ACC/' datafile > output. However, in my data file there are a number of occurrences of ABB... (4 Replies)
Discussion started by: pauli
4 Replies
Login or Register to Ask a Question