- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Found a word
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 02:57 AM
тАО11-28-2005 02:57 AM
Found a word
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 03:26 AM
тАО11-28-2005 03:26 AM
Re: Found a word
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 03:29 AM
тАО11-28-2005 03:29 AM
Re: Found a word
(That's all on one line by the way.)
Possibly, not sure if I understand your question fully.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 03:30 AM
тАО11-28-2005 03:30 AM
Re: Found a word
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 03:30 AM
тАО11-28-2005 03:30 AM
Re: Found a word
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 03:31 AM
тАО11-28-2005 03:31 AM
Re: Found a word
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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 03:39 AM
тАО11-28-2005 03:39 AM
Re: Found a word
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 03:48 AM
тАО11-28-2005 03:48 AM
Re: Found a word
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
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 04:34 AM
тАО11-28-2005 04:34 AM
Re: Found a word
awk '$0~/2\/6/ {print substr($0,match($0,/ARASW/),7)}'
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 07:12 AM
тАО11-28-2005 07:12 AM
Re: Found a word
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...