LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 08-27-2008, 12:21 PM   #1
visitnag
Member
 
Registered: Mar 2008
Posts: 147

Rep: Reputation: 15
grepping from a particular key positions of a file.


I have a file of million records with 253 character length of each record. I want to grep a particular word from character position 180 to 220 and print the whole record. How?
 
Old 08-27-2008, 12:40 PM   #2
Fantasio
Member
 
Registered: Nov 2007
Location: Vinsobres - Drome - France
Distribution: SuSE Linux 11.3
Posts: 148

Rep: Reputation: 18
while read ligne
do
echo $ligne | cut -c 180-220 | grep "your_keyword"
[ $? -eq 0 ] && echo $ligne
done
 
Old 08-27-2008, 12:41 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Are your records line-limited (i.e. 253 characters per line, with a new line at the end of the line?

Can you provide an example of a record?

If the file is not line de-limited, but a fixed record size file, I would probably go for a small Perl program, which would read records with the sysread function, and extract this part of the record using substr. A match can then be determined against the substring, and if it is found, whatever action you want to happen can be performed.
 
Old 08-27-2008, 01:08 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by visitnag View Post
I have a file of million records with 253 character length of each record. I want to grep a particular word from character position 180 to 220 and print the whole record. How?
The way this is phrased implies that:
a) you want to remove the particular word and then print the record.
OR maybe:
b) you want to print only the records where the particular word is found.

Please clarify and give an example....
 
Old 08-28-2008, 10:32 AM   #5
Fantasio
Member
 
Registered: Nov 2007
Location: Vinsobres - Drome - France
Distribution: SuSE Linux 11.3
Posts: 148

Rep: Reputation: 18
Oops I 've forgotten to precise after "done" your file as input.

while read ligne
do
echo $ligne | cut -c 180-220 | grep "your_keyword"
[ $? -eq 0 ] && echo $ligne
done < filename
 
Old 08-30-2008, 07:00 PM   #6
visitnag
Member
 
Registered: Mar 2008
Posts: 147

Original Poster
Rep: Reputation: 15
hi all,

To fantasio:

Your shell is not giving any result when i tried to run it. I hav to use ctrl+c to come out from the shell. Try to rectify the error and also explain me amout [ $? -eq 0 ], as I am poor in Shell scripts(mostly i use awk). I am using RHEL 4 Es. Thank you,

To rest:

Here is an example.

6006458515285 20080831 156458522456484MIKEMCDOWN565845685ANTANIO MIKE SANJOSE CA1542365413201
6006458515285 20080831 156458522456484MIKEMCDOWN565845685ANTANIO CROW SANJOSE CA1542365413201

The above lineS are some part of two records(every record is separated with a new line char). In the above lines i want to grep MIKE starting character from ANTONIO upto end of SANJOSE characters. MIKE is repeated in the second line but it is not available in my required character place. so that is to be omitted. When i apply grep MIKE it should grep only line number one.

I got the result in awk by changing the required characters into small case and grepped. It is like this.

awk '{l=substr($0,1,(one letter before antonio))tolower(substr($0,from antononio to,upto end of CA)substr($0,after CA, to rest all)}{print l}' inputfile > outputfile
grep mike outputfile.
But i want the above solution in shell.

Thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grepping a log file graziano1968 Linux - General 11 12-08-2011 02:52 AM
Grepping a file for Text going down. keysorsoze Linux - Newbie 3 01-17-2007 09:06 AM
Saving Window Positions? kraydl Mandriva 2 11-16-2005 11:51 AM
Icon Positions robertop Linux - Newbie 7 09-25-2004 06:29 PM
Window Positions in KDE mooreted Mandriva 2 03-13-2004 07:29 PM


All times are GMT -5. The time now is 07:01 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration