Solved

Field extractions using sed

Posted on 2013-02-05
3
226 Views
Last Modified: 2013-02-06
Could someone please put me out of my mystery and give me a suitable sed expression to extract fields 4 and 5 from the following,

1.2.3.4 [05/Feb/2013:06:25:29 +0000] 0.034 0.070 - 200 "Cache-Control: no-cache, must-revalidate, max-age=0"

this will result in output
0.034 0.070

Thanks
0
Comment
Question by:brothertom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 38858559
STRING='1.2.3.4 [05/Feb/2013:06:25:29 +0000] 0.034 0.070 - 200 "Cache-Control: no-cache, must-revalidate, max-age=0"'

echo $STRING |sed 's/^.*\] //; s/ -.*$//'

Same with "perl:

echo $STRING |perl -pe 's/^.*\] //; s/ -.*$//'

"awk" will do as well:

echo $STRING |awk -F"] | -" '{print $2}'

or "cut":

echo $STRING | cut -f2 -d"]" |cut -f1 -d"-" |cut -f2-3 -d" "

Finally, variable editing:

STRING=${STRING##*] }; STRING=${STRING%% -*}; echo $STRING

P.S.: A "grep" solution, just for fun:

echo $STRING | grep -oE "[0-9]*\.[0-9]{3} [0-9]*\.[0-9]{3}"
0
 

Author Closing Comment

by:brothertom
ID: 38858855
Cool, thanks
0
 
LVL 12

Expert Comment

by:tel2
ID: 38861819
Nice work, wmp!

My only comment is that the cut solution:
    echo $STRING | cut -f2 -d"]" |cut -f1 -d"-" |cut -f2-3 -d" "
could have been simply:
    echo $STRING | cut -d" " -f4-5
which is shorter (and possibly more efficient) than all the other solutions.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AWS CLI - Instances, Volumes & Tagging 2 154
Need some help with regex 3 58
AD Cleanup by EmployeeID 11 81
Powershell command 2 53
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question