1827791 Members
2630 Online
109969 Solutions
New Discussion

Help Script

 
SOLVED
Go to solution
Vogra
Regular Advisor

Help Script

Hi All!

I need to select a field on the first line of block of two lines, but on second line is the fied that I can use to identify it.

Like this:

13245 MANDF #12345 15678
ford my@id.com

---
I need the filed number 3 (#12345).

There are a lot of lines like this.

Any idea?
Thanx,
Lima.
We are spirits in the material world
3 REPLIES 3
RAC_1
Honored Contributor

Re: Help Script

so you want as follows
If line has "ford my@id.com"
you want one line above and thrid feild from it??
There is no substitute to HARDWORK
harry d brown jr
Honored Contributor

Re: Help Script

#!/usr/bin/perl
#
#
$prevline = "";
#
#
while () {
if (/my\@id.com/) {
($fld1,$fld2,$fld3,$fld4) = split(/ /,$prevline);
printf("%s\n",$fld3);
}
$prevline = $_;
}

chmod a+x perlscript
cat filename | perlscript

live free or die
harry d brown jr
Live Free or Die
Hein van den Heuvel
Honored Contributor
Solution

Re: Help Script


awk '{old=new; new=$3}/ford/{print old}' data-file

ford is an example selection string.
replace with 'my@id.com' or whatever needed.

if the data line is recognizable for example by the 'MANDF' or the #, then you can optimize to:

awk '/#/{old=$3}/ford/{print old}' data-file

Hein.