The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
string deletion, variable contents, fixed delimiters rebelbuttmunch Shell Programming and Scripting 2 03-24-2009 07:44 AM
c program to extract text between two delimiters from some text file kukretiabhi13 High Level Programming 7 12-03-2008 06:29 PM
How to fetch data from a text file in Unix shikhakaul Shell Programming and Scripting 4 01-25-2008 11:20 AM
convert XML file into Text file(fixed length) ram2s2001 Shell Programming and Scripting 0 11-03-2005 01:28 AM
Inserting new line after match of a fixed string sunil_neha Shell Programming and Scripting 6 04-13-2004 12:09 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2009
nareshk nareshk is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
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 (permalink)  
Old 05-27-2009
Rhije Rhije is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 103
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 (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
  #4 (permalink)  
Old 05-27-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 534
Quote:
Originally Posted by nareshk View Post
...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..
  #5 (permalink)  
Old 05-27-2009
nareshk nareshk is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
Quote:
Originally Posted by durden_tyler View Post
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!
  #6 (permalink)  
Old 05-27-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Code:
grep "\<0000001234\>" file > newfile

-Devaraj Takhellambam
  #7 (permalink)  
Old 05-27-2009
Rhije Rhije is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 103
Well, you can also just do:

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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0