Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Remove from character to end of line

by sdyates (Scribe)
on Aug 01, 2002 at 16:39 UTC ( [id://186828]=perlquestion: print w/replies, xml ) Need Help??

sdyates has asked for the wisdom of the Perl Monks concerning the following question:

This should be easy, but cannot quite do it.

here is the file:

9077/00:00///:8087
9079/00:00/////386/_

here is what I want

9077
9079

herre is my logic:
- remove from first / to end of line
- remove / charcter.

I am using system command. I am not sure how to tell to remove from the first character to end of line.

sed 's/\/*//g' pslog3


I've done it before, but I do not have my perl reference book here!!

please help

Replies are listed 'Best First'.
Re: Remove from character to end of line
by aufrank (Pilgrim) on Aug 01, 2002 at 16:57 UTC
    I would probably try a pattern like
    s$\/+.*$$
    which matches at least one forward slash and then any characters after it. note that perlstyle recommends that you not use a forward slash as the delimiter for any substitution or match involving slashes, but I don't know if that works from the command line, so use whatever delimiter is most friendly with your shell. I'm sure some regex guru will be along soon to provide a better answer, but this gets the job done at least (I think).

    hth

    --au
Re: Remove from character to end of line
by $name (Pilgrim) on Aug 01, 2002 at 16:47 UTC
    You could do somthing like

    my $filename = "9077/00:00///:8087"; my @file = split('/', $filename); my $newname = $file[0];

    MGW Aplications Developer QuinnTeam Inc.
Re: Remove from character to end of line
by particle (Vicar) on Aug 01, 2002 at 16:54 UTC
    my( $answer ) = ( $data =~ m!^(.+?)/!);

    ~Particle *accelerates*

Re: Remove from character to end of line
by CukiMnstr (Deacon) on Aug 01, 2002 at 16:50 UTC
    you have the s/// operator in perl. this should do what you want:
    while(<>) { s#^([^/]+).*#$1#; print; }

    hope this helps,

    Update: even if the s/// works, split() is faster:

    while(<>) { print((split '/')[0], "\n"); }

    Update: ++Abigail-II. I need to work on my golfing skills... ;)

      But that has to capture. You can do without capture:
      s!/.*!!;
      It's smaller code too. ;-)

      Abigail

        GREAT!!!

        Not only smaller code, but smart code too. I knew there had to be an easy way. It is amazing how dumb I feel with out my reference books.

        Thanks much,
        Simon
Re: Remove from character to end of line
by Tomte (Priest) on Aug 01, 2002 at 16:56 UTC
    my @lines = qw[ 9077/00:00///:8087 9079/00:00/////386/_]; my @erg; foreach (@lines){ s/([^\/]*)//; push @erg, $1; } # erg contains all segments you wanted

    HTH
    regards,
    tomte

    update: damn, whenever I wrote and tested my examples, there are a bunch of so much cooler solutions....way to go tomte, way to go...:-)

    update_ii: s/$1 @erg/@erg, $1/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://186828]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found