Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Wish to keep only those lines which start with letter s, and delete all other lines.

by Anonymous Monk
on Dec 06, 2001 at 03:27 UTC ( [id://129799]=perlquestion: print w/replies, xml ) Need Help??

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

I have a large file, and I wish to keep only those lines which start with the letter s. I am a beginer,Please advice me how should I go about it. Thanks
  • Comment on Wish to keep only those lines which start with letter s, and delete all other lines.

Replies are listed 'Best First'.
Re: Wish to keep only those lines which start with letter s, and delete all other lines.
by Beatnik (Parson) on Dec 06, 2001 at 03:42 UTC
    perl -pi.bak -e'if (!/^s/) { $_ = "" }' filename in shell

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Wish to keep only those lines which start with letter s, and delete all other lines.
by Fastolfe (Vicar) on Dec 06, 2001 at 03:47 UTC
    If you're on Unix, you don't necessarily need Perl for this at all:
    $ egrep -v ^s input-file
Re: Wish to keep only those lines which start with letter s, and delete all other lines.
by Chmrr (Vicar) on Dec 06, 2001 at 04:10 UTC

    Another varient on the -pi.bak -e ("pi bake") schemata, but using the powers of the "ni bake":

    perl -ni.bak -e'print if /^s/'

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^+*`^ ve^#$&V"+@( NO CARRIER'

Re: Wish to keep only those lines which start with letter s, and delete all other lines.
by wog (Curate) on Dec 06, 2001 at 04:27 UTC
    Golfing a bit:

    perl -i -ne'/^s/i..print' # or if we don't care about capital s... perl -i -ne'/^s/..print'

    update: s/-ine/-i -ne/g # stupid mistake

      Did someone say golf? ;-)
      perl -i -pe'$_ x=/^s/i'

      -Blake

Re: Wish to keep only those lines which start with letter s, and delete all other lines.
by lestrrat (Deacon) on Dec 06, 2001 at 03:46 UTC

    caution: the following code does an in-place edit. make sure to back up ( well, the -i.bak does it for you, but it can't hurt to be careful )

    perl -i.bak -pe 's/^[^s].*\z//' file
Re: Wish to keep only those lines which start with letter s, and delete all other lines.
by vek (Prior) on Dec 06, 2001 at 06:00 UTC
    substr anyone? Assuming file already opened successfully...
    while ( <THEFILE> ) { next if ( substr ( $_, 0, 1 ) =~ /s/i ); # now do stuff with the lines you do want }
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-25 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found