Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing Question

Status
Not open for further replies.

Newbee21369

Programmer
Oct 13, 2004
30
0
0
US
I'm trying to parse each path that is found between: /usr/ AND /tape/
while ignoring the case.

1. /usr/tmhp/data/mark/Erco/from_Erco/archive/tape/501046515504800.x12
2. /usr/ltmhprod/data/GIS/MorganS/From_MorganS/Archive/tape/5010465155048.x12
3. /usr/ltmhprod/data/CI/From_CI/Archive/tape/50104651550.x12

I'm expecting the following results:

1. tmhp/data/market/Erco/from_Erco/archive
2. ltmhprod/data/GIS/MorganS/From_MorganS/Archive
3. ltmhprod/data/CI/From_CI/Archive

Any suggestions on how to do this? Thanks in Advance.
 
tmtowtdi

Code:
while(<DATA>){ # read from list at end of script
	if(m[/usr/]){ # is the string '/usr/' in there?
		$_ = $'; # yes, save everything after it
		if(m[/tape/]){ # is the string '/tape/' in there?
			$_ =$`; # yes, save everything before it
			print "$_\n"; # print what's left
		}
	}
}

__END__
/usr/tmhp/data/mark/Erco/from_Erco/archive/tape/501046515504800.x12
/usr/ltmhprod/data/GIS/MorganS/From_MorganS/Archive/tape/5010465155048.x12
/usr/ltmhprod/data/CI/From_CI/Archive/tape/50104651550.x12


Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
if all the lines have /usr/ and /tape/ :

Code:
while(<DATA>){
   $_ = substr $_,5,(rindex($_,'/tape/')-5);
   print "$_\n";
}

if they all don't have /usr/ and /tape/

Code:
while(<DATA>){
   next unless ( index($_,'/usr/')>-1 and index($_,'/tape/')>-1);
   $_ = substr $_,5,(rindex($_,'/tape/')-5);
   print "$_\n";
}
 
Well yes, but mine has more nesting :)

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Code:
while (<DATA>) {
   @data=split /\/usr\/|\/tape\// ;
   push(@paths,$data[1]);
}
foreach (@paths) { print "$_\n" }
assuming /usr/ and /tape/
TIMOWTDI

--Paul

cigless ...
 
If they don't all have /usr/ and /tape/:
Code:
use strict;
use warnings;

while (<DATA>) {
    print "$1\n" if (m[^/usr/(.+)/tape/]);
}

__DATA__
/usr/tmhp/data/mark/Erco/from_Erco/archive/tape/501046515504800.x12
/usr/ltmhprod/data/GIS/MorganS/From_MorganS/Archive/tape/5010465155048.x12

Header record with unfortunate usr and tape combination
/usr/ltmhprod/data/CI/From_CI/Archive/tape/50104651550.x12
 
Guys,
What about a one liner based on stevexff's idea?
Code:
perl -lne 'print "$1" if (m[^/usr/(.+)/tape/]);'
Never forget the art of a "one-liner". ;-)


Trojan.
 
This one was the one I could understand the most and worked
the best for me. Thanks to everyone for your input!!!!

if($filepath =~ m/usr/i) # is the string '/usr/' in there?
{
$_ = $'; # yes, save everything after it

if($filepath=~ m/tape/i)# is the string '/tape/' in there?
{
$_ =$`; # yes, save everything before it
my $PATH_TX = $_."tape/"; ## do concatenation
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top