-1

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?

Aris Pratama
  • 3
  • 1
  • 3
  • 1
    As written this make no sense. Do you want the number after size=? – John3136 Jun 15 '17 at 02:32
  • Possible duplicate of [Grep: Capture just number](https://stackoverflow.com/questions/27827023/grep-capture-just-number) – GilZ Jun 15 '17 at 11:53

6 Answers6

1

with sed:

awk '{print $8}' tes.txt|sed 's/.*size=\(.*\),$/\1/'
tso
  • 4,732
  • 2
  • 22
  • 32
0

To extract the part behind =, pipe the output to cut:

awk '{print $8}' tes.txt | cut -d= -f2
Chris Lam
  • 3,526
  • 13
  • 10
0
awk '{split ($8, s, "="); print s[2]}' tes.txt
output: 21341
KI-YOUNG BANG
  • 199
  • 1
  • 10
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 15 '17 at 15:10
0

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.

CWLiu
  • 3,913
  • 1
  • 10
  • 14
0
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.

https://www.gnu.org/software/grep/manual/grep.html

AkihikoTakahashi
  • 159
  • 2
  • 10
0

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.

JFS31
  • 518
  • 5
  • 13