awk pattern search with inconsistent position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk pattern search with inconsistent position
# 1  
Old 09-13-2013
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 05:08 AM..
# 2  
Old 09-13-2013
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
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 09-13-2013
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
This User Gave Thanks to apmcd47 For This Post:
# 4  
Old 09-13-2013
Quote:
Originally Posted by ahamed101
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
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 ?
# 5  
Old 09-13-2013
+ one or more hit
* 0 or more hit
? 0 or 1 hit
# 6  
Old 09-15-2013
Quote:
Originally Posted by apmcd47
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.
# 7  
Old 09-17-2013
Quote:
Originally Posted by Jin_
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk -Search pattern through Variable

Hello, We have wrote shell script for multiple file name search pattern. file format: <numner>_<20180809>.txt starting with single number and ending with 8 digits number Command: awk -v string="12_1234" -v serch="^+_+$" "BEGIN{ if (string ~/serch$/) print string }" If sting matches... (4 Replies)
Discussion started by: koti_rama
4 Replies

2. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

3. Shell Programming and Scripting

awk Index to get position matches pattern

Input data as below (filetest.txt): 1|22 JAN Minimum Bal 20.00 | SAT 2|09 FEB Extract bal 168.00BR | REM 3|MIN BAL | LEX Output should be: ( If there is Date & Month in 2nd field of Input file, It should be seperated else blank. If There is Decimal OR Decimal & Currency in last of the 2nd... (7 Replies)
Discussion started by: JSKOBS
7 Replies

4. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

5. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

6. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

7. UNIX for Dummies Questions & Answers

Pattern search using awk

Hi All, I am trying to find numbers with balance greater than 1 and less than equal 2 from the below file using awk Input file num ,bal 100199,1.708 100225,0 100226,0 100228,0.771166 100232,2 output file 100199,1.708 100232,2 I am using the following command for this... (2 Replies)
Discussion started by: pistachio
2 Replies

8. Shell Programming and Scripting

Pattern Search using AWK

Hi All, I have the below file data.txt.Using awk i want to grep all the zone data.Form the below command i can extact data upto of zone i give but i want it should print until next pattern. awk '/^Total Collection /{c=5;next}c-->0' zin45srs08.tools_utilization instead of c=5 is it possible... (5 Replies)
Discussion started by: ajaincv
5 Replies

9. Shell Programming and Scripting

awk search pattern

Hi, I have a log file which contains lines like below: 2010-07-19 07:13:19,021 ERROR system ...(text) 2010-07-19 07:22:03,427 ERROR system ...(text) class com... (text) 2010-07-19 07:23:19,026 ERROR system ...(text) class com... (text) each line is a separate line... I am given the a... (14 Replies)
Discussion started by: a27wang
14 Replies

10. Shell Programming and Scripting

How to awk/sed/grep lines which contains a pattern at a given position

Dear friends I am new to linux and was trying to split some files userwise in our linux server. I have a data file of 156 continuous columns named ecscr final. I want the script to redirect all the lines containing a pattern of 7 digits to separate files. I was using grep to do that,... (2 Replies)
Discussion started by: anoopvraj
2 Replies
Login or Register to Ask a Question