Solved

Field extractions using sed

Posted on 2013-02-05
3
211 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

776 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