Solved

Field extractions using sed

Posted on 2013-02-05
3
193 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
Comment Utility
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
Comment Utility
Cool, thanks
0
 
LVL 11

Expert Comment

by:tel2
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now