Q: Search after a match once per line I'm trying to create a script to get all the Apache response after specific word "statusDescription", but I have a problem that I got the output duplicated for some rows as matched word or response maybe written 2 times @ the same line 
Sample of log "1 line":
GET/en?status=1&newMainBalance=5486&serviceAmount=700&ExternalTrxId=asdf&PgTrxId=tfpsadf&amount=0&statusDescription=Failed&customerCode=1.1&newDedicatedBalance=0&secureHash=56a7sdyf&paidAmount=1000&responseMsg=%a1%a1%A1(PG_ID)&language=enHTTP/1.1"200186243**1/1210669**1"-""-""-""https://example.com.eg?statusDescription=Failed&externalTrxId=123&status=203&secureHash=asdf&pgTrxId=asdf
i tried the below command to get any match between "statusDescription" and "&"
cat test.txt  | perl -nle'print $1 while /statusDescription(.*?)\&/g'
cat test.txt  | perl -nle'print $1 while /statusDescription(.*?)\&/g'

output:

=Failed
  =Failed

i except the result to be 1 line only like

=Failed

or 

=Failed=Failed


A: The -l is having the effect of printing a line feed after each print.
The following will print the value of the (first) statusDescription field of the requested URL (while ignoring the referrer URL). It will even correctly decode the value for you (if it contains escaped characters such as + or %20).
perl -MURI -MURI::QueryParam -nle'
   my ($request_url) = /^\S+\s+(\S+)/
      or next;
   $request_url = URI->new($request_url, "http");
   my $status = $request_url->query_param("statusDescription")
      or next;
   print $status;
' test.txt


A: You used -l option without a value. To quote from perlrun:

If octnum is omitted, sets $\ to the current value of $/

which by default is \n. Hence every print will automatically get a \n appended.
$  echo 'Match=once& should only Match=twice& once' | perl -nle 'print "MATCH ->$1<-" while /Match(.*?)&/g'
MATCH ->=once<-
MATCH ->=twice<-

# the same without -l
$ echo 'Match=once& should only Match=twice& once' | perl -ne 'print "MATCH ->$1<-" while /Match(.*?)&/g'
MATCH ->=once<-MATCH ->=twice<-

# if your post-processing requires matches from different lines to appear
# on separate lines then append a conditional print "\n" at the end
$ echo 'Match=once& should only Match=twice& once' | perl -ne 'print "MATCH ->$1<-" while /Match(.*?)&/g; print "\n" if $1'
MATCH ->=once<-MATCH ->=twice<-
$ echo 'nomatch=once& should only nomatch=twice& once' | perl -ne 'print "MATCH ->$1<-" while /Match(.*?)&/g; print "\n" if defined $1'
$

For $/ (input record separator) and $\ (output record separator) see also perlvar.
