Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Simple perl one-liner for transforming text files

by hiseldl (Priest)
on Mar 06, 2003 at 03:23 UTC ( #240789=note: print w/replies, xml ) Need Help??


in reply to Simple perl one-liner for transforming text files

Here's another version. When running one-liners it is extremely useful to learn the command line operators. Here's a short breakdown:

  • use -aF: to split on the ':' and store it in @F
  • use -l to automatically chomp $/
  • use -0 (minus zero) to specify the input record separator ($/) as the null character
  • use -n to loop over every line of input
  • use -e to run the script following it
  • the first expression prints the counter if the line contains name
  • the second expression prints the right side of the ':' if the line contains name or date
  • the last expression prints a newline (or record separator) if the line contains date

 perl -al0nF: -e 'print /name/&&++$x,/name|date/&&$F[1],/date/&&$/' data.txt

the data file contains:

name: url1.domain1.com date: 2004/2/1 unwanted info: blah blah blah name: url2.domain5.org date: 2004/3/2 unwanted info: blah blah blah name: url3.domain1.com date: 2004/2/3 unwanted info: blah blah blah name: url4.domain5.org date: 2004/3/4 unwanted info: blah blah blah name: url5.domain1.com date: 2004/2/5 unwanted info: blah blah blah name: url6.domain5.org date: 2004/3/6 unwanted info: blah blah blah

and here are the results of running the script:

1 url1.domain1.com 2004/2/1 2 url2.domain5.org 2004/3/2 3 url3.domain1.com 2004/2/3 4 url4.domain5.org 2004/3/4 5 url5.domain1.com 2004/2/5 6 url6.domain5.org 2004/3/6

Change all the ' into " to run on win32

HTH.

--
hiseldl
What time is it? It's Camel Time!

Replies are listed 'Best First'.
Re: Re: Simple perl one-liner for transforming text files
by DaveH (Monk) on Mar 06, 2003 at 09:09 UTC

    Do sick minds think alike? It would seem we came up with very similar answers!

    Cheers,

    -- Dave :-)


    $q=[split+qr,,,q,~swmi,.$,],+s.$.Em~w^,,.,s,.,$&&$$q[pos],eg,print

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2022-05-19 02:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?