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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find on line 1

Status
Not open for further replies.

mte0910

Programmer
Apr 20, 2005
232
US
Dumb question:
This line...
s/,.*$//;
finds the data from the start of the sentence up to the first "," in encounters, and disregards everything after. What if I want to do the exact same thing, but starting at the second ","?
 
I may have answered part of my question, but it isn't doing what I expect...

Given:
agriggs,10.160.4.148,tcmcpccma1,agriggs
tthomas,10.160.7.14,tcmcpccma1,tthomas
sturman,10.160.4.186,tcmcpccma1,sturman

Code:
s/,{2}.*$//;
I expected the {2} to start from the second iteration of "," but it is not. Do you see what is wrong?
 
this means two commas in succession:

,{2}

as in ',,'

it does not mean the second occurance of a comma anywhere in the line/string.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'll buy that, so how do I find the second instance of a comma?
 
maybe this is what you are trying to do:

Code:
s/^[^,]+,[^,]+,(.*)/$1/;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You could also use the split() function but I am unsure what you are trying to do in the end so its hard to say which way to go, pure regexp or split().

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin, thanks for your help, I am definately getting closer to the answer. I apprishiate your patience with me.


Here is the raw data:
agriggs,10.160.4.148,tcmcpccma1,agriggs
tthomas,10.160.7.14,tcmcpccma1,tthomas
sturman,10.160.4.186,tcmcpccma1,sturman


Here is the expected result:
agriggs,10.160.4.148
tthomas,10.160.7.14
sturman,10.160.4.186


Here is my code:
open (IN, 'P:\CCMAAudit\tmp00.txt');
open (OUT,'>P:\CCMAAudit\tmp01.txt');
while (<IN>) {
chomp;
s/^[^,]+,[^,]+,(.*)/$1/;
print OUT $_, "\n";
}
close (IN); close (OUT);


Here is the actual result:
tcmcpccma1,agriggs
tcmcpccma1,tthomas
tcmcpccma1,sturman


I'm getting the exact opposite of what I'm after. I'm close to understanding this, but I just haven't wrapped my head around it yet.
 
change the regexp to:

Code:
s/^([^,]+,[^,]+),.*/$1/;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You're welcome, thanks for the
star.gif


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top