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!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.