awk pattern search with inconsistent position | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

awk pattern search with inconsistent position

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-13-2013
Jin_ Jin_ is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 30 October 2013, 2:48 AM EDT
Posts: 26
Thanks: 16
Thanked 0 Times in 0 Posts
awk pattern search with inconsistent position

Hi,

Anybody knows how to get the value after the regexp and test it on pattern? The if the pattern matches, it will print the entire line on a separate file.

Here's my raw file:

Code:
^_Name^_string^_Apple   ^_Color^_string^_Red	^_Code^_string^_121 
^_Name^_string^_Banana	^_Code^_string^_123    ^_Color^_string^_Yellow  
^_Name^_string^_Citrus	^_Color^_string^_Green	^_Code^_string^_129    ^_Color^_string^_Green

I want to check if the Code string's last digit is within range of [1-5]. The Code^_ is not always on the same column so I can't just use the awk $3.

eg. I specified to search for [1-5], the output will be below:

Code:
^_Name^_string^_Apple   ^_Color^_string^_Red	^_Code^_string^_121  
^_Name^_string^_Banana	^_Code^_string^_123    ^_Color^_string^_Yellow

eg. I specified to search for [19], the output will be below:

Code:
^_Name^_string^_Apple   ^_Color^_string^_Red	^_Code^_string^_121 
^_Name^_string^_Citrus	^_Color^_string^_Green	^_Code^_string^_129    ^_Color^_string^_Green

What I've done is to get the code values,store in a file then grep [range]. But looping takes huge time specially with large files.

By the way, the "^_" is a control character, and the spaces are tabs.

---------- Post updated at 03:58 PM ---------- Previous update was at 03:52 PM ----------

I can also use grep without looping, but I cant use the * in between so no. Here's what's in my mind:


Code:
grep Code^_string^_*[1-5]$ [filename]


Last edited by Jin_; 09-13-2013 at 04:08 AM..
Sponsored Links
    #2  
Old 09-13-2013
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 31 October 2014, 5:13 AM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 486 Times in 479 Posts
Something like this?

Code:
grep "_Code^_string^_[0-9]\{2\}[1-5]" infile

grep "_Code^_string^_[0-9]\{2\}[19]" infile

How many digits will you have (the above is for 3 digits)? And I suppose you want to check only the last digit?

--ahamed
The Following User Says Thank You to ahamed101 For This Useful Post:
Jin_ (09-13-2013)
Sponsored Links
    #3  
Old 09-13-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts
If you modify that grep to:

Code:
grep Code^_string^_[0-9]+[1-5]

it should work for when only numerals appear after the Code/string construct. I have not tested this.

Andrew
The Following User Says Thank You to apmcd47 For This Useful Post:
Jin_ (09-13-2013)
    #4  
Old 09-13-2013
Jin_ Jin_ is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 30 October 2013, 2:48 AM EDT
Posts: 26
Thanks: 16
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by ahamed101 View Post
Something like this?

Code:
grep "_Code^_string^_[0-9]\{2\}[1-5]" infile

grep "_Code^_string^_[0-9]\{2\}[19]" infile

How many digits will you have (the above is for 3 digits)? And I suppose you want to check only the last digit?

--ahamed
Digits are inconsistent as well. Can be 3, can be 10. Is the solution above going to work?

Quote:
Originally Posted by apmcd47 View Post
If you modify that grep to:

Code:
grep Code^_string^_[0-9]+[1-5]

it should work for when only numerals appear after the Code/string construct. I have not tested this.

Andrew

What does the "+" sign do? Is it like the "*" in ls ?
Sponsored Links
    #5  
Old 09-13-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
+ one or more hit
* 0 or more hit
? 0 or 1 hit
Sponsored Links
    #6  
Old 09-15-2013
Jin_ Jin_ is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 30 October 2013, 2:48 AM EDT
Posts: 26
Thanks: 16
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by apmcd47 View Post
If you modify that grep to:

Code:
grep Code^_string^_[0-9]+[1-5]

it should work for when only numerals appear after the Code/string construct. I have not tested this.

Andrew
Problem is when the line also has irrelevant numbers at the end of the line, it will give a hit to the grep.
Sponsored Links
    #7  
Old 09-17-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts
Quote:
Originally Posted by Jin_ View Post
Problem is when the line also has irrelevant numbers at the end of the line, it will give a hit to the grep.
You have tested this? As I said, I had not. Taking another look I can see that it could match something like:

Code:
Code^_string^_12127

To stop this you would need to add something to the end of the pattern to match white space, but this would not match the end of the line. You could add a match for white space to the end of the pattern but then it won't match the end of the line. You could have two versions of my pattern: one with white space a the end and the other with the end of line anchor ($) at the end.

Andrew
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Awk to match a pattern and perform a search after the first pattern RickCharles Shell Programming and Scripting 8 09-06-2012 11:21 PM
Pattern search using awk pistachio UNIX for Dummies Questions & Answers 2 07-26-2011 04:04 PM
Pattern Search using AWK ajaincv Shell Programming and Scripting 5 06-20-2011 04:13 AM
awk search pattern a27wang Shell Programming and Scripting 14 09-20-2010 04:17 PM
How to awk/sed/grep lines which contains a pattern at a given position anoopvraj Shell Programming and Scripting 2 10-29-2008 10:23 AM



All times are GMT -4. The time now is 04:17 AM.