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!

Delete after X in text file 1

Status
Not open for further replies.

mte0910

Programmer
Apr 20, 2005
232
US
ActiveState Perl v5.10. on Windows 2003

Q: I'm trying to find a particular place in a text file, in this case I'm looking for the ":". I want to delete everything after that.

before
one two: three four
five six seven: eight
nine: ten

after
one two
five six seven
nine

I was trying to use substring and index, but I am not going about it right because I keep getting invalid argument. I'm ready to give up.
 
post your perl code

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I don't even have anything viable at this point. I may be fighting the system itself. I tried setting something up that was basic...

my $try = index('barfoobar', 'foo');
print $try "\n";

to test the index function to see what I got and what it did, but even this gives me the invalid argument response.
 
The only problem with that code is the print line lacks a comma:

Code:
my $try = index('barfoobar', 'foo');
print $try, "\n";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Code:
[kirsle@eclipse ~]$ cat <<EOF > before.txt
> one two: three four
> five six seven: eight
> nine: ten
> EOF
[kirsle@eclipse ~]$ perl
open (FROM, "before.txt");
open (TO, ">after.txt");

while (<FROM>) {
	chomp;
	s/^([^:]+?):.*?$/$1/ig;
	print TO $_, "\n";
}

close (TO);
close (FROM);
__END__
[kirsle@eclipse ~]$ cat after.txt 
one two
five six seven
nine
[kirsle@eclipse ~]$

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top