i want to ask a question about 'awk' , example:
awk '{print $8}' tes.txt
output : size=21341,
but i only want to grep number "21341", can you give me solution about how to grep the number?
i want to ask a question about 'awk' , example:
awk '{print $8}' tes.txt
output : size=21341,
but i only want to grep number "21341", can you give me solution about how to grep the number?
To extract the part behind =
, pipe the output to cut
:
awk '{print $8}' tes.txt | cut -d= -f2
awk '{split ($8, s, "="); print s[2]}' tes.txt
output: 21341
You may use the function sub
in awk
to do that.
awk '{sub(/^.*=/,"",$8);print $8}' tes.txt
sub(/^.*=/,"",$8)
: Eliminate the pattern before "=", and you would get the numeric part.
awk '{print $8}' tes.txt | grep -o '[0-9]*'
output: 21341
grep -o <regex>
prints only the matched parts of matching lines, with each such part on a separate output line. See more detail.
Here is a grep solution:
grep -oP '(?<=size=)\d+' file
At the moment it searches the whole file so if you want to restrict it to one specific line you have to use awk before.