Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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!


In reply to Re: Simple perl one-liner for transforming text files by hiseldl
in thread Simple perl one-liner for transforming text files by m5m5m

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-24 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found