- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: 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
Forums
Discussions
Discussions
Discussions
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
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2005 08:04 PM
11-28-2005 08:04 PM
Re: Found a word
We just want lines containing 2/6
Surround ARASW the only characters
are ( and )
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2005 08:11 PM
11-28-2005 08:11 PM
Re: Found a word
# cat file1
2/6 ARASW25 TOTO tutu
(ARASW26) 2/6 TOTO xyz 12/x
tutu yy uu 6/8 ARASW56 titi fff
# grep "2/6" file1 | grep '^(ARASW[^ ]*)'
(ARASW26) 2/6 TOTO xyz 12/x
If it is not the requirement revert with more details.
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2005 09:14 PM
11-28-2005 09:14 PM
Re: Found a word
Please try the second solution I posted above. Based on your input, in my opinion, it *does* give you exactly the output for which you asked.
Cut-and-paste the script I posted in my *second* post, name it "perl.pl" and call it like:
# ./perl.pl 2/6 yourfile
The first argument (for example, 2/6, tells that you only want to find lines with this token in them.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2005 09:41 PM
11-28-2005 09:41 PM
Re: Found a word
# grep $MODPORT fic | awk '{ for (i=1;i
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2005 09:54 PM
11-28-2005 09:54 PM
Re: Found a word
sed -e "/2\/6/!d;s/.*\(ARASW[^) ]*\).*/\1/;"
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2005 04:52 AM
11-29-2005 04:52 AM
Re: Found a word
Based on your reply here's the complete awk construct...
# awk '$0~/ 2\/6 /||$0~/^2\/6 /||$0~/ 2\/6$/{print substr($0,match($0,/ARASW/),7)}'
cheers!