0

Consider a log file which contains

r100000|Tom Sawyer|2010-12-01|view.txt

I should parse this and print

ID:r100000
NAME:Tom Sawyer
DATE:2010-12-01
FILENAME:view.txt

I should only use regular expressions.

5 Answers 5

4
$line = 'r100000|Tom Sawyer|2010-12-01|view.txt';
@fields = split /\|/, $line;
print $fields[0]; # r100000
Sign up to request clarification or add additional context in comments.

Comments

2

the easier way is to break your string into fields, using delimiters. Since you have pipe "|" as delimiters, then use it. No need for complicated regex. Plus, what if you have more fields next time?.

Here's one with awk (you can use -F option of Perl as well)

$ awk -F"|" '{print "ID:"$1" Name:"$2" Date:"$3" filename:"$4}' file
ID:r100000 Name:Tom Sawyer Date:2010-12-01 filename:view.txt

Perl equivalent

$ perl -F"\|" -ane 'print "ID:$F[1] Name: $F[2] Date:$F[3] filename:$F[4]"' file
ID:Tom Sawyer Name: 2010-12-01 Date:view.txt

Comments

2

If you want to use Regular expression to parse it,

you can try this one:

$line = r100000|Tom Sawyer|2010-12-01|view.txt;

if($line =~ /^([^|]+)\|([^|]+)\|([^|]+)\|([^|]+)$/)
{
$id = $1;
$name = $2;
$date = $3;
$filename = $4
}

Comments

0

You don't mention the dialect of RE. But for instance:

$ echo 'r100000|Tom Sawyer|2010-12-01|view.txt' | \
  perl -pe 's/^(r\d+)\|([^|]+)\|([0-9-]+)\|(.+)/ID:\1 NAME:\2 DATE:\3 FILENAME:\4/'
ID:r100000 NAME:Tom Sawyer DATE:2010-12-01 FILENAME:view.txt

Comments

0
use Data::Dumper;
my %h;
my $line = 'r100000|Tom Sawyer|2010-12-01|view.txt';
@h{qw/ID NAME DATE FILENAME/} = (split /\|/, $line);
print Dumper(\%h);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.