I have following result.log file (always has 2 lines) which I need to process,
cat result.log
name.cmd.method,"result","abc","xyz";
name="hello,mine.12345,"&"tree"&" xyz "&" tree "&" xyz",
data="way,"&" 1"&"rate-me"&"1"&"rate-me",str="",ret="";
now I need to extract the strings/data as below onto a file
data|name|occurance
way|hello.mine.12345|1
1|tree|2
rate-me|xyz|2
You say 'always has 2 lines', but I count three.
Can it be depended on to always have the same arrangement?
Which xyz is the one you're wanting? Your example input has that in 3 different places.
Its two lines only, some how when I typed i did newline for second line.
No, I am not after xyz.
Ok to make it clear I will be having data in two fields: DATA and NAME both will be having equal number of strings and each string will be separated by "&".
Now I need NAME|DATA|OCCURRENCE. In DATA and NAME the strings can be repeated multiple times so I need to do duplicate on NAME|DATA and count that write it to FILE.
NAME|DATA|OCCURRENCE
expect|foo|2
session|major|1
baddata|minor|1
For above data File will be having DATA="foo"&"major"&"foo"&"minor",NAME="expect"&"session"&"expect"&"baddata";
How about this ?
#!/usr/bin/perl
while (<DATA>) {
chomp;
@fields=split(/,/);
@D=split(/=/,$fields[0]);
@N=split(/=/,$fields[1]);
@N_V=split(/\&/,$N[1]);
@D_V=split(/\&/,$D[1]);
for($i=0;$i<=$#D_V;$i++) {
$N_V[$i]=~s/"//g;
$D_V[$i]=~s/"//g;
$has_d{$N_V[$i]}++;
$has_n{$N_V[$i]}=$D_V[$i];
}
}
print "NAME|DATA|OCCURRENCE \n";
foreach (keys %has_n)
{
print "$_ | $has_n{$_} | $has_d{$_} \n";
}
__DATA__
DATA="foo"&"major"&"foo"&"minor",NAME="expect"&"session"&"expect"&"baddata"
What's your actual data look like then?