Extracting data from a text file using awk [duplicate]
Possible Dupl开发者_StackOverflow中文版icate:
Extract data between two points in a text file
For example:
Reply: [200/OK] bytes=29086 time=583ms
I would want to extract the value between "time=" and "ms"
Expected Result:
"583"
I would use sed for that, but since you ask for awk:
echo "Reply: [200/OK] bytes=29086 time=583ms" | awk -F'time=|ms' '{print $2}'
The -F
defines extended regexp for field separator. So we define that "time=" or "ms" separates the fields, and then print second field.
using sed, it would be:
echo "Reply: [200/OK] bytes=29086 time=583ms" | sed 's/.*time=\([0-9]*\)ms.*/\1/'
echo "Reply: [200/OK] bytes=29086 time=583ms" | sed "s/.*time=\(.*\)ms/\1/"
Ugly but works:
$ echo "Reply: [200/OK] bytes=29086 time=583ms" |
awk '{print $4'} | sed -e 's/[a-z=]//g'
583
Or without sed
:
$ echo "Reply: [200/OK] bytes=29086 time=583ms" |
awk '{ split($4,a,"="); gsub(/ms/,"", a[2]); print a[2] }'
583
Try this
printf "Reply: [200/OK] bytes=29086 time=583ms\n" \
| awk '/^Reply:/{sub(/^.*time=/,"",$0) ;sub(/ms$/,"",$0); print $0}'
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.
精彩评论