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
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 Section
Seekers of Perl Wisdom