Extracting lines that match string at certain position


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extracting lines that match string at certain position
# 1  
Old 06-09-2008
Extracting lines that match string at certain position

I have a fixed length file in the following format

<date><product_code><other data>

The file size is huge and I have to extract only the lines that match a certain product code which is of 2 bytes length. I cannot use normal grep since that may give undesirable results. When I search for prod cd 20, if I give grep 20, it matches with date 2008... Since this is a fixed length file, I know the position where to search. Is there any command to search at particular position and extract the matching lines?
# 2  
Old 06-09-2008
use the following command :

awk 'substr($0,9,2)=="02" {print}' <filename>
# 3  
Old 06-09-2008
That works fine. Much appreciated.

I am curious to know. Are there any alternate ways to do it other than using awk? Like sed, grep or cut. Do any other unix utility provide the option of searching at a particular position and length?
# 4  
Old 06-09-2008
Code:
sed -n -e 's/^[0-9]\{8\}20*/&/p' file

# 5  
Old 06-09-2008
Is there a generic syntax of this?

sed -n -e 's/^[0-9]\{8\}20*/&/p' file

I understand as you are skipping until 8th position and do a pattern search with 20*. I do not understand how the [0-9] and & help the search.
# 6  
Old 06-09-2008
Hammer & Screwdriver solution with grep

Code:
> cat parts
01010602grape
02020701apple
02020602banana
05250607pear
12250702melon
01010805banana02
10130202kiwi

Code:
> cat parts | grep "^......02"
01010602grape
02020602banana
12250702melon
10130202kiwi

Note the
. to wildcard for a character position
^ to wildcard starting at left column
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

2. Shell Programming and Scripting

Remove lines that match string at end of column

I have this: 301205 0000030000041.49000000.00 2011111815505 908 301205 0000020000029.10000000.00 2011111815505 962 301205 0000010000027.56000000.00 2011111815505 3083 312291 ... (2 Replies)
Discussion started by: herot
2 Replies

3. Shell Programming and Scripting

Find the position of lines matching string

I have a file with the below format, GS*8***** ST*1******** A* B* E* RMR*123455(This is the unique number to locate this row) F* SE*1*** GE** GS*9***** ST*2 H* J* RMR*567889(This is the unique number to locate this row) L* SE* GE***** (16 Replies)
Discussion started by: Muthuraj K
16 Replies

4. Shell Programming and Scripting

BASH: extracting values from multiple lines after a match

Hi there, I have the following output, # raidctl -l RAID Volume RAID RAID Disk Volume Type Status Disk Status ------------------------------------------------------ c0t1d0 IM OK c0t1d0 OK ... (4 Replies)
Discussion started by: rethink
4 Replies

5. Shell Programming and Scripting

Extracting several lines of text after a unique string

I'm attempting to write a script to identify users who have sudo access on a server. I only want to extract the ID's of the sudo users after a unique line of text. The list of sudo users goes to the EOF so I only need the script to start after the unique line of text. I already have a script to... (1 Reply)
Discussion started by: bouncer
1 Replies

6. Shell Programming and Scripting

Extracting N lines match number X of a pattern

Hi All, as the title says I need to extract N lines after match number X of a pattern. e.g. 111 xxx xxx 111 yyy yyy 111 www www 111 zzz zzz I would like to extract the two lines after the second 111 occurrence. I tried with grep but I didn't find any options to do that. Any... (11 Replies)
Discussion started by: f_o_555
11 Replies

7. UNIX for Dummies Questions & Answers

Extracting m lines after n lines after match

Hi All, I would like to extract from a text file m lines skipping n lines after a string occurrency. Is it possible with grep? e.g. qqq ww eee rrr ttt yyy uuu I want to print 2 lines skipping 1 line after the string 'ww' result would be rrr ttt (2 Replies)
Discussion started by: f_o_555
2 Replies

8. Shell Programming and Scripting

help extracting a matching pattern and next lines of match

Hi there, i'm having some problems just making an awk script (i've tried this way, but other way can be posible for sure), for the next file file.txt <register> <createProfile> <result>0</result> <description><!]></description> <msisdn>34661461174</msisdn> <inputOmvID>1</inputOmvID>... (6 Replies)
Discussion started by: vicious
6 Replies

9. Shell Programming and Scripting

How can I match lines with just one occurance of a string in awk?

Hi, I'm trying to match records using awk which contain only one occurance of my string, I know how to match one or more (+) but matching only one is eluding me without developing some convoluted bit of code. I was hoping there would be some simple pattern matching thing similar to '+' but... (9 Replies)
Discussion started by: jonathanm
9 Replies

10. Shell Programming and Scripting

Print lines with search string at specific position

Hi Folks, I have a file with all fields defined by byte position, but any field can be empty so I cannot print lines based on a search of specific columns. I need to print all lines of this file where the string of two characters at byte position 100-101 contains the number 27. Any ideas? ... (4 Replies)
Discussion started by: HealthyGuy
4 Replies
Login or Register to Ask a Question