Solved

Field extractions using sed

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…

920 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