LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grepping from a particular key positions of a file. (https://www.linuxquestions.org/questions/linux-newbie-8/grepping-from-a-particular-key-positions-of-a-file-665823/)

visitnag 08-27-2008 12:21 PM

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?

Fantasio 08-27-2008 12:40 PM

while read ligne
do
echo $ligne | cut -c 180-220 | grep "your_keyword"
[ $? -eq 0 ] && echo $ligne
done

matthewg42 08-27-2008 12:41 PM

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.

pixellany 08-27-2008 01:08 PM

Quote:

Originally Posted by visitnag (Post 3261669)
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....

Fantasio 08-28-2008 10:32 AM

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

visitnag 08-30-2008 07:00 PM

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.


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