Operating System - HP-UX
1830354 Members
2590 Online
110001 Solutions
New Discussion

Capturing a word in a line of text whose placement moves

 

Capturing a word in a line of text whose placement moves

I'm trying to write a script and need assistance in figuring out how to capture a word in each line of text when that word changes position. Below are the result of my cat | grep 'CHECK: #'
What I want to do then is to extract just the check number from each line. The problem is that the placement of the check number varies depending on the length of the name, so just using the awk print function doesn't always capture the correct field. Is there a command that I can use to locate the field # so that I can then grab the proceeding check number?

OWNER: 59020 1949 INC CHECK: #185841 06/17/05

OWNER: 30252 1995 INCOME PROGRAM LIMITED CHECK: #185842 06/17/05
OWNER: 12129 A G ANDRIKOPOULOS RESOURCES INC CHECK: #185843 06/17/05
OWNER: 52984 A V M INC CHECK: #185844 06/17/05
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: Capturing a word in a line of text whose placement moves

How about using awk and setting the FS (field separator) to # ?


Pete

Pete
Mel Burslan
Honored Contributor

Re: Capturing a word in a line of text whose placement moves

I am not a PERL expert but I know this is a task for using PERL on the very basic level. The command you are looking for is 'index' but again I am the worst person to advise you when the subject is PERL.

HTH
________________________________
UNIX because I majored in cryptology...
Mel Burslan
Honored Contributor

Re: Capturing a word in a line of text whose placement moves

well, just to give you an idea

create a file findpos.pl as follows

#!/usr/bin/perl
$a=;
$b=index $a, "word3";
printf $b

chmod 755 findpos.pl

echo "word1 word2 word3 word4 word5" | ./findpos.pl

12


hope this helps


________________________________
UNIX because I majored in cryptology...
A. Clay Stephenson
Acclaimed Contributor

Re: Capturing a word in a line of text whose placement moves

Okay, let's do it closer to the real Perl Cowboy Way:

perl -n -e 'if (/#(\d+)/) { print $1,"\n"; }' < myfile

You might want to make the pattern match a bit more foolproof by:
perl -n -e 'if (/CHECK:#(\d+)/) { print $1,"\n"; }' < myfile


The \d+ says match all the digits you can; expressions after the matching string in paren's are assigned $1 $2 ... but we are looking for only the number so there is just one paren'ed pattern. Perl is really pattern matching on steroids. I haven't tested this but if I didn't make no booboo's this here should work.


If it ain't broke, I can fix that.
RAC_1
Honored Contributor

Re: Capturing a word in a line of text whose placement moves

Fast and dirty.

cat file | tr " " "\n" | egrep -i "check|#[0-9]"

Anil
There is no substitute to HARDWORK
H.Merijn Brand (procura
Honored Contributor

Re: Capturing a word in a line of text whose placement moves

Even shorter

# perl -nle'/CHECK:\s*#(\d+)/ and print$1' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: Capturing a word in a line of text whose placement moves

sed:

cat filename | sed -e "s/\(.*\)CHECK: \#\([0-9]\{1,10\}\) \([0-9].*$\)/\2/"

live free or die
harry d brown jr
Live Free or Die

Re: Capturing a word in a line of text whose placement moves

Thanks for your suggestions. The perl commands worked best for me particular issue.

Re: Capturing a word in a line of text whose placement moves

Thank you all of your suggestions. The perl command worked best.