Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Field extractions using sed

Posted on 2013-02-05
3
Medium Priority
?
233 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

688 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