Operating System - HP-UX
1833867 Members
2278 Online
110063 Solutions
New Discussion

How to grep a formatted file for matching text exact positions

 
SOLVED
Go to solution
Declan Mc Kay
Occasional Advisor

How to grep a formatted file for matching text exact positions

Hi, I can't seem to figure a way to extact specific lines from a formatted file which would match a search string. I would like to extract all lines that match text at a specific position in the file.
In example below I just want to print out lines that match
the field which begins at posiotion 21 in each line e.g. 5398888500404
From below, from the three line sample, two lines would match....Hope you can understand what I need to do..?
010220030326030735065398888500404 01200012030102
010220030326030735065398888500505 20302302030230
010220030326030735065398888500404 23232232323233
4 REPLIES 4
John Meissner
Esteemed Contributor

Re: How to grep a formatted file for matching text exact positions

cat file |
while read line
do
var1=$(echo $line | cut 21-33)
if [ $var1 = 5398888500404 ]
then
echo $var1 >> file2
fi
done
All paths lead to destiny
Rodney Hills
Honored Contributor

Re: How to grep a formatted file for matching text exact positions

You could use grep-

grep "^....................5398888500404" file

Kind of kludgy because you have to count out "." to the position you want.

You could use awk-

awk 'substr($0,21,13)=="5398888500404"{print $0}' file

You could use perl-

perl -n -e 'print $_ if substr($_,20,13) eq "5398888500404"'

HTH

-- Rod Hills
There be dragons...
John Meissner
Esteemed Contributor

Re: How to grep a formatted file for matching text exact positions

Sorry - I messed up the previous post... one of the variables was wrong.


cat file |
while read line
do
var1=$(echo $line | cut 21-33)
if [ $var1 = 5398888500404 ]
then
echo $line >> file2
fi
done
All paths lead to destiny
Frank Slootweg
Honored Contributor
Solution

Re: How to grep a formatted file for matching text exact positions

I think this does what you want:

grep '^.\{20,20\}pattern'

^ is the start of the line
. is any character
\{20,20\} means 20 repetitions of the previous regular expression, i.e. 20 any-characters

pattern is the real pattern to be grep(1)-ped.

I.e. for example for your example data and pattern:

$ cat message | grep '^.\{20,20\}5398888500404'
010220030326030735065398888500404 01200012030102
010220030326030735065398888500404 23232232323233
$