26

My query is to extract the value between double quotes "". Sample input is:

10.219.41.68 - - - [11/Jun/2014:10:23:04 -0400] Sec:0 MicSec:1797 "GET /balancer-manager HTTP/1.1" 200 28980 "-" "curl/7.15.5 (i386-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5"

I have large log files, so values can be vary for each line, need to extract the value between first occurrence of double quotes…

Expected output:

GET /balancer-manager HTTP/1.1

Anyone have any idea then please suggest.

phk
  • 5,953
  • 7
  • 42
  • 71
user79658
  • 271

5 Answers5

37

You can just use cut for this:

$cut -d '"' -f2 < logfile
GET /balancer-manager HTTP/1.1

-d '"' tells cut to use a double quote as its field delimiter. -f2 tells it to take the second field, which is between the first and second quotes - or the first quoted string, exactly what you want.

Michael Homer
  • 76,565
  • How to print all even numbered columns in cut easily like %!cut -d '"' -f2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58? – hhh Jul 20 '17 at 18:46
13

A way using awk

awk -F'"' '$0=$2' file

If for some absurd reason your HTTP methods are actually 0 and you want to output these

awk -F'"' '{$0=$2}1' file
phk
  • 5,953
  • 7
  • 42
  • 71
4

You can do it many ways.

With awk:

$ awk -F'"' '{print $2}' file
GET /balancer-manager HTTP/1.1

With perl:

$ perl -F'"' -anle 'print $F[1]' file
GET /balancer-manager HTTP/1.1
cuonglm
  • 153,898
4

Since an awk and perl solutions are already provided, I wanted to try sed:

sed 's/[^"]*"\([^"]*\)".*/\1/' file
phk
  • 5,953
  • 7
  • 42
  • 71
Srini
  • 161
1

processing quoted input numbers

echo   1234   | awk '{                i=strtonum($1) ;  printf( "%s %d\n",$1, i)}'  # no problem
echo '"1234"' | awk '{                i=strtonum($1) ;  printf( "%s %d\n",$1, i)}'  # does not work
echo '"1234"' | awk '{ gsub("\"",""); i = $1         ;  printf( "%s %d\n",$1, i)}'  # works
A-Man
  • 11