Grep in a file for a particular pattern in a particular position witihn the file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep in a file for a particular pattern in a particular position witihn the file
# 1  
Old 03-25-2010
Grep in a file for a particular pattern in a particular position witihn the file

Assume I have a file with a lot of data sets like
Code:
123 abc 01
456 def 02
789 ghi

and I only want to grep all that datasets from my file having the pattern '02' at the postion 9-10 to get only
Code:
456 def 02

So I could group the datsets into three files according to the position 9-10, one for '01', one for '02' and one for ' '

Thanks in advance for any help Smilie
# 2  
Old 03-25-2010
something to start with:
Code:
nawk '{if ($3 !=file){close(file);file=$3}{print $0 > file}' myFile

# 3  
Old 03-25-2010
that was really a FAST respond, I'll try it right now! Smilie
# 4  
Old 03-25-2010
Code:
sed -n 's/........02.*/&/p' file
sed -n -r 's/.{8}02.*/&/p' file

# 5  
Old 03-25-2010
Sorry but it didn't work. The position I mentioned was just an example, acctually it is changing and not a separate word. In addition my file is a compressed file. Smilie

So how can I look up in a zipped file whether a particluar position matches my pattern and to return those datasets completely (no cutting)

Thanks again Smilie
# 6  
Old 03-25-2010
Quote:
Originally Posted by ABE2202
Sorry but it didn't work. The position I mentioned was just an example, acctually it is changing and not a separate word. In addition my file is a compressed file. Smilie

So how can I look up in a zipped file whether a particluar position matches my pattern and to return those datasets completely (no cutting)

Thanks again Smilie
well.. you have to know the starting/ending position if you cannot rely on the field numbering scheme.
's' for start. 'l' for 'length'
Code:
nawk '{f=(substr($0,s,l);if (f !=file){close(file);file=f}{print $0 > file}' s=9 l=2 myFile

# 7  
Old 03-26-2010
thanks for that code, but it seems I am to inept for handling and understanding this Smilie
Code:
nawk '{f=(substr($0,s,l);if (f !=file){close(file);file=f}{print $0 > file}' s=487 l=2 myfile

could you please tell me if and how the following replaced?

Code:
 
f
file
myfile

I didin't understand how to use your template!? Smilie

---------- Post updated at 09:27 AM ---------- Previous update was at 09:18 AM ----------

it returns
Code:
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
        awk: 0602-540 There is a missing } character.
        awk: 0602-540 There is a missing ) character.

but I don't now where to put the missing '}' or ')' if there is one missing
and I still don't know where to set my input-file and output-file in that command
thank you and sorry Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a pattern in a key position a file

Hi guys, So I have a file containing data of a marathon. Here's an example what it looks like including the given key: # key: sex, time, athlete, athlete's nationality, date, city, country M, 2:30:57.6, Harry Payne, GBR, 1929-07-05, Stamford Bridge, England M, 2:5:42, Khalid Khannouchi,... (3 Replies)
Discussion started by: DNM_UKN
3 Replies

2. 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

3. Shell Programming and Scripting

Grep the 5th and 6th position character of a word in a file

I am trying to find/grep the 5th and 6th position character (TX) of a word in a file. I tried to do grep "....TX" file The output gives me everything in the file with TX in it. I only need the output with the TX in the 5th and 6th position of the word. Any idea Example: test1 car... (5 Replies)
Discussion started by: e_mikey_2000
5 Replies

4. UNIX for Dummies Questions & Answers

find the file names having specified pattern at specified position in the current directory

I would need a command for finding first 15000 of the file names whose 25th postion is 5 in the current directory alone. I do have this painful command find . -name '5*' | head -15000 | cut -c3- please refine this. Of course the above command also searches in the sub directories... (3 Replies)
Discussion started by: vk39221
3 Replies

5. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

6. Shell Programming and Scripting

Find the position of a pattern on a line from a csv file

hello I'm doing a unix program and i'm using many file csv.in each csv file the colums are separated by ";" I would like to know the position of a pattern. For example for a line yyyy, bbbb, cccc; ddddd;eeee. I will like for example by finding the position of the pattern "cccc" and the response is... (6 Replies)
Discussion started by: papis
6 Replies

7. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

8. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

9. Shell Programming and Scripting

File find | xargs grep for pattern file

Folks I've been struggling this with for far too liong now and need your help! I've been happily using grep for a search of a directory, to list the files which contain a string: find . -type f -mtime -5 -print | xargs grep -l 'invoiceID=\"12345\"' Now the list of 'invoiceID' I am... (4 Replies)
Discussion started by: daveaasmith
4 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