1753875 Members
7519 Online
108809 Solutions
New Discussion юеВ

Found a word

 
iranzo
Frequent Advisor

Found a word

Hello,

How to extract a word or a field in a file
depending on the position of another word, field ?

variable:
MODPORT="2/6"

here the text file :
2/6 ARASW25 TOTO tutu
(ARASW26) 2/6 TOTO xyz 12/x
tutu yy uu 6/8 ARASW56 titi fff

with a command like:
grep $MODPORT fic | ....

We want to extract only the words beginning
by "ARASW" with the variable "MODPORT":
ARASW25
ARASW26

Thanks a lot.(if you understand my problem)
Bonjour
15 REPLIES 15
A. Clay Stephenson
Acclaimed Contributor

Re: Found a word

You have managed to confuse me and I never solve a problem until I understand it.

You said for input:
2/6 ARASW25 TOTO tutu
(ARASW26) 2/6 TOTO xyz 12/x
tutu yy uu 6/8 ARASW56 titi fff

You want:
ARASW25
ARASW26

Note that "(ARASW26)" does not start with "ARASW" but is included in the output; however, "ARASW56" does start with "ARASW" but is not included in the output.

Grep will not do this but sed, perl, or awk can easily grab the field. You should also tell us whether "ARASW" appears at most once in any given line of input. If more than once then what should the output be?
"ARASW"
If it ain't broke, I can fix that.
Stephen Keane
Honored Contributor

Re: Found a word

grep $MODPORT fic | sed -e "s/^.*ARASW/ARASW/" -e "s/[^[:alnum:]].*//"

(That's all on one line by the way.)

Possibly, not sure if I understand your question fully.
A. Clay Stephenson
Acclaimed Contributor

Re: Found a word

Oh, I see, you first want to find any line containing "2/6" but this is flawed as well. What about lines containing "12/6", "2/60", etc. Are the only characters which could surround your "ARASW", '(' and ')'?
If it ain't broke, I can fix that.
Jeff Schussele
Honored Contributor

Re: Found a word

Hi,

grep can only handle entire lines (records) by itself. awk can handle words (fields) in addition to records
So you need to use a combination of grep & awk to do this.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
James R. Ferguson
Acclaimed Contributor

Re: Found a word

Hi:

Perhaps this helps:

# cat /tmp/perl.pl
#!/usr/bin/perl -w
$x=shift;
$f=shift;
open( FH, "<", $f ) or die "Can't open $f: $!\n";
while () {
if (m%$x%go) {
print "$`" if defined $`;
print "$'" if defined $';
}
}

Run as:

# /tmp/perl.pl 2/6 file

...where "2/6" is your MODPORT variable value and "file" is your input.

Regards!

...JRF...
Kent Ostby
Honored Contributor

Re: Found a word

I'm going to post two things. The first will pull the words that CONTAIN ARASW as follows.

Create a file called pullit.awk and put the following lines in it:

/ARASW/ {idx1=index($0,"ARASW");
sbs1=substr($0,idx1);
idx2=index(sbs1," ");
if (idx2==0) {print sbs2;next;};
print substr(sbs1,1,idx2);
}

Run the command:

awk -f pullit.awk < inputfile

Output will be:

ARASW25
ARASW26)
ARASW56
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Kent Ostby
Honored Contributor

Re: Found a word

Oops .. okay .. see that by re-reading this, I see that you also want the line to have the same value as $MODPORT.

so here is what I would do.

Create a file pullit.sh

echo $MODPORT > useme
cat textfile >> useme
awk -f pullit.awk < useme

Create another file called pullit.awk:

NR==1 {checkvalue=$0;next;}
index($0,checkvalue)==0{next}
/ARASW/ {idx1=index($0,"ARASW");
sbs1=substr($0,idx1);
idx2=index(sbs1," ");
if (idx2==0) {print sbs2;next;};
print substr(sbs1,1,idx2);
}

This will yield:
ARASW25
ARASW26)

If you know that you will haev ARASW?? then you can use this for pullit.awk instead:

NR==1 {checkvalue=$0;next;}
index($0,checkvalue)==0{next}
/ARASW/ {idx1=index($0,"ARASW");
print substr($0,idx1,7);
}

This will yield:

ARASW25
ARASW26
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Sandman!
Honored Contributor

Re: Found a word

How about the awk construct below:

awk '$0~/2\/6/ {print substr($0,match($0,/ARASW/),7)}'

cheers!
James R. Ferguson
Acclaimed Contributor

Re: Found a word

Hi Iranzo:

This gives you exactly what you described whether or not your "word" occurs before or after the 'MODPORT' variable's value. It's a generalized script to show the word that preceeds or follows the token specified:

# cat /tmp/perl.pl
#!/usr/bin/perl
use strict;
use warnings;
my ($x, $f, $part1, $part2, @flds);
$x=shift;
$f=shift;
open( FH, "<", $f ) or die "Can't open $f: $!\n";
while () {
if (m%$x%go) {
($part1 = $`) =~ s/[()]//g;
($part2 = $') =~ s/[()]//g;
if ($part1 =~ m/\s+/) {
print "$part1\n";
} else {
@flds = split / /, $part2;
print "$flds[1]\n";
}
}
}
1;

--cut--

NOTE that the "word" that occurs before or after 'MODPORT' is printed. Given your input, you will see your requested output.

As before, run as:

# MODPORT="2/6";/tmp/perl.pl ${MODPORT} filename

Regards!

...JRF...