Hi All ,
I have a file with below data
# User@Host: xyz @ [ipaddress]
# Query_time: t1 Lock_time: t2 Rows_sent: n1 Rows_examined: n2
SET timestamp=1396852200;
select count(1) from table;
# Time: 140406 23:30:01
# User@Host: abc @ [ipaddress]
# Query_time: t1 Lock_time: t2 Rows_sent: n1 Rows_examined: n2
SET timestamp=1396852201;
Select max(value) from table;
I would like an output some thing like below
#User@Host:xyz@[ipaddress] | Query_Time: t1 | Lock_time: t2 | Rows_Sent: n1 | Rows_examined: n2 | SET timestamp=1396852200; | select count(1) from table;|# Time: 140406 23:30:01
#User@Host:xyz@[ipaddress] | Query_Time: t1 | Lock_time: t2 | Rows_Sent: n1 | Rows_examined: n2 | SET timestamp=1396852201; | Select max(value) from table;
I tried paste option which did not work fine as the lines between same value are changing .
perl -ne '
if(m/User@Host/){s/\s//g; if($.>1){print "\n"}; print "$_ | "}
elsif((@m = m/\w+:\s\w+/g) && (0+@m>2)){print join(" | ",@m) }
else {chomp; print " | $_"}
' file
Thank you Jethrow for the code , I am still very new to Perl I am fiddling around . Could you let me know what wrong I am doing when executing the query .
perl -ne '
if(m/User@Host/){s/\s//g; if($.>1){print "\n"}; print "$_ | "}
elsif((@m = m/\w+:\s\w+/g) && (0+@m>2)){print join(" | ",@m) }
else {chomp; print " | $_"}
' /home/rpagadala/temp1
Error
syntax error at test.pl line 2, near "-ne"
Execution of test.pl aborted due to compilation errors.
Jethrow's code worked for me, but I couldn't copy and paste the whole thing at once. When I copied line by line it ran and gave me:
#User@Host:xyz@[ipaddress] | Query_time: t1 | Lock_time: t2 | Rows_sent: n1 | Rows_examined: n2 | SET timestamp=1396852200; | select count(1) from table; | # Time: 140406 23:30:01
#User@Host:abc@[ipaddress] | Query_time: t1 | Lock_time: t2 | Rows_sent: n1 | Rows_examined: n2 | SET timestamp=1396852201; | Select max(value) from table;
---------- Post updated at 03:43 PM ---------- Previous update was at 03:21 PM ----------
rakesh_411, I can't send private messages until my post count is 10, so hopefully this isn't inappropriate...
For the record, I'm not really a perl guy either.
I simply copied and pasted each line of jethrow's code one by one:
perl -ne '
Enter
if(m/User@Host/){s/\s//g; if($.>1){print "\n"}; print "$_ | "}
Enter
elsif((@m = m/\w+:\s\w+/g) && (0+@m>2)){print join(" | ",@m) } else {chomp; print " | $_"}
Enter
else {chomp; print " | $_"}
Enter
' file
Enter
And I got the output that I posted above.
Thanks Blake , I was able to get the output by running at the command line however if I save it in a file and execute it I am getting error . Let me know if any changes need to be done if I had to save the same query in a file and execute it .
It is worthless to mention an error and then not report it accurately. Copy/paste the exact version of the script you're using along with the exact error messages generated. Without that information, you're wasting everyone's time (including your own).
Also, it can only help if you specify which operating system platform you're working with.
Regards,
Alister
Hello Alister,
Sorry for my delay response , I am running Linux (2.6) . Below is the error message I am receiving when running as
perl test.pl
syntax error at test.pl line 1, near "-ne"
Execution of test.pl aborted due to compilation errors.
However when I execute as below its running fine .
./test.pl
Again I apologies for not providing right information .