The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 05-27-2009
nareshk nareshk is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
Quote:
Originally Posted by Rhije View Post
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