Fetch the rows with match string on a fixed lenth text file - NO delimiters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch the rows with match string on a fixed lenth text file - NO delimiters
# 1  
Old 05-27-2009
Fetch the rows with match string on a fixed lenth text file - NO delimiters

Hi

I am trying to fetch the rows with match string "0000001234"

Input file looks like below:

09 0 XXX 0000001234 Z 1
09 0 XXX 0000001234 Z 1
09 0 XXX 0000001234 Z 1
09 0 XXX 0000001234 Z 1
09 0 XXX 0000001234 Z 1
09 0 XXX 0000001234 Z 1

here the scenario is like we need to fetch the rows with match string "0000001234" and print the lines in a separate file ...

i tried with grep command by grep ^09 file > output file it works fine only when the string is starts first.

Please can some one help me how we can do this ...
# 2  
Old 05-27-2009
grep is going to be working on a line by line basis, so grep ^09 is doing just what you asked it to do by finding any line that begins with 09

You probably want to use awk, it would be the easiest thing to do. If the data is in the same format that you provided, you could do something like the following:

Code:
awk '$4 == "0000001234"' file

By default (if you do not tell awk to print anything specific), it will print the entire record/row. So the above will only print a row if the fourth field is what is shown.

You could also use word boundaries in grep:

Code:
-bash-3.2$ cat test.txt
09 0 XXX 0000001234 Z 1
09 0 XXX 0000001234 Z 1
09 0 XXX 50000001234 Z 1
09 0 XXX 40000001234 Z 1
09 0 XXX 30000001234 Z 1
09 0 XXX 10000001234 Z 1

-bash-3.2$ grep "\<0000001234\>" test.txt
09 0 XXX 0000001234 Z 1
09 0 XXX 0000001234 Z 1

Hope that helps.
# 3  
Old 05-27-2009
Code:
grep "\<0000001234\>" file > newfile


-Devaraj Takhellambam
# 4  
Old 05-27-2009
Quote:
Originally Posted by Rhije
grep is going to be working on a line by line basis, so grep ^09 is doing just what you asked it to do by finding any line that begins with 09

You probably want to use awk, it would be the easiest thing to do. If the data is in the same format that you provided, you could do something like the following:

Code:
awk '$4 == "0000001234"' file

By default (if you do not tell awk to print anything specific), it will print the entire record/row. So the above will only print a row if the fourth field is what is shown.

You could also use word boundaries in grep:

Code:
-bash-3.2$ cat test.txt
09 0 XXX 0000001234 Z 1
09 0 XXX 0000001234 Z 1
09 0 XXX 50000001234 Z 1
09 0 XXX 40000001234 Z 1
09 0 XXX 30000001234 Z 1
09 0 XXX 10000001234 Z 1

-bash-3.2$ grep "\<0000001234\>" test.txt
09 0 XXX 0000001234 Z 1
09 0 XXX 0000001234 Z 1

Hope that helps.

Thanks for your Help

what if the input file is like this

09 0 01000000001234 Z 1
09 0 01000000001234 Z 1
09 0 010050000001234 Z 1
09 0 010040000001234 Z 1
09 0 010030000001234 Z 1
09 0 010010000001234 Z 1

and now i want to fetch the rows with match string "0000001234" i.e search the string from 10th column to 19 th column and fetch the rows
# 5  
Old 05-27-2009
Quote:
Originally Posted by nareshk
...what if the input file is like this

09 0 01000000001234 Z 1
09 0 01000000001234 Z 1
09 0 010050000001234 Z 1
09 0 010040000001234 Z 1
09 0 010030000001234 Z 1
09 0 010010000001234 Z 1

and now i want to fetch the rows with match string "0000001234" i.e search the string from 10th column to 19 th column and fetch the rows
Something like this:

Code:
$
$ awk 'substr($0,10,10) == "0000001234"' input.txt
09 0 01000000001234 Z 1
09 0 01000000001234 Z 1
$


tyler_durden

Last edited by durden_tyler; 05-27-2009 at 06:34 PM..
# 6  
Old 05-27-2009
Well, you can also just do:

Code:
awk '$3 ~ /0000001234/' file

# 7  
Old 05-27-2009
Quote:
Originally Posted by durden_tyler
Something like this:

Code:
$
$ awk 'substr($0,10,10) == "0000001234"' input.txt
09 0 01000000001234 Z 1
09 0 01000000001234 Z 1
$

tyler_durden
Thanks guys !

This helped me a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

UNIX command -Filter rows in fixed width file based on column values

Hi All, I am trying to select the rows in a fixed width file based on values in the columns. I want to select only the rows if column position 3-4 has the value AB I am using cut command to get the column values. Is it possible to check if cut -c3-4 = AB is true then select only that... (2 Replies)
Discussion started by: ashok.k
2 Replies

3. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match. I have tried this command but it does not work when I put the command in a loop in a bash script: ... (12 Replies)
Discussion started by: jojojmac5
12 Replies

4. Shell Programming and Scripting

Append spaces the rows to make it into a required fixed length file

I want to make a script to read row by row and find its length. If the length is less than my required length then i hav to append spaces to that paritucular row. Each row contains special characters, spaces, etc. For example my file contains , 12345 abcdef 234 abcde 89012 abcdefgh ... (10 Replies)
Discussion started by: Amrutha24
10 Replies

5. Shell Programming and Scripting

Getting a string without fixed delimiters

I have a line of text for example aaaa bbbb cccc dddd eeee ffffff I would need to get the cccc however bbbb could be there or not. So whether bbbb is in the line or not I need cccc. I was looking at either awk or sed....and trying to start at c and end until the next space. Also... (11 Replies)
Discussion started by: bombcan1
11 Replies

6. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

7. Shell Programming and Scripting

Program to insert Delimiters at fixed locations in a file, Can you please Debug it for me??

Can someone please help?I have a file - fixed.txt----------------------------AABBBBCCCCCCDDDEEFFFFGGGGGGHHHIIJJJJKKKKKKLLL----------------------------To insert delimiters at fixed lengths of 2, 4, 6, 3, I created a file text1.txt as-------------------2463----------------------and trying to execute... (10 Replies)
Discussion started by: jd_mca
10 Replies

8. Shell Programming and Scripting

string deletion, variable contents, fixed delimiters

Hi, I need to mass delete the following string(s) from my files weight=100, However the '100' is variable e.g Current: ---------------- moretext=bar, weight=100, moreinfo=blah extrastuff=hi, weight=9999, extrainfo=foo Desired: ------------------ moretext=bar, moreinfo=blah... (2 Replies)
Discussion started by: rebelbuttmunch
2 Replies

9. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

10. Shell Programming and Scripting

Inserting new line after match of a fixed string

Hi, I have a file which contains many occurances of a string say "hellosunil". I want to insert a newline charcater after all the "hellosunil" strings in the file. trying to use sed, sed -e 's/hellosunil/\\nhellosunil/g' file1 sed help says u cannot substitute a regular expression... (6 Replies)
Discussion started by: sunil_neha
6 Replies
Login or Register to Ask a Question