1

I have a log file which contains lines like the following one:

Internal (reserved=1728469KB, committed=1728469KB)

I'd need to extract the value contained in "committed", so 1728469 I'm trying to use awk for that

cat file.txt | awk '{print $4}' 

However that produces:

committed=1728469KB)

This is still incomplete and would need still some work. Is there a simpler solution to do that instead? Thanks

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Carla
  • 3,064
  • 8
  • 36
  • 65
  • `awk -F= '{sub(/[)]/,"",$3); print $3}` might help. Good luck – shellter Sep 09 '20 at 13:31
  • See also: https://stackoverflow.com/questions/3070141/how-do-i-use-grep-to-extract-a-specific-field-value-from-lines , https://stackoverflow.com/questions/5080988/how-to-extract-string-following-a-pattern-with-grep-regex-or-perl – Sundeep Sep 09 '20 at 14:18

4 Answers4

5

Could you please try following, using match function of awk.

awk 'match($0,/committed=[0-9]+/){print substr($0,RSTART+10,RLENGTH-10)}' Input_file

With GNU grep using \K option of it:

grep -oP '.*committed=\K[0-9]*' Input_file

Output will be 1728469 in both above solutions.

1st solution explanation:

awk '                                      ##Starting awk program from here.
match($0,/committed=[0-9]+/){              ##Using match function to match from committed= till digits in current line.
  print substr($0,RSTART+10,RLENGTH-10)    ##Printing sub string from RSTART+10 to RLENGTH-10 in current line.
}
' Input_file                               ##Mentioning Input_file name here.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
4

Sed is better at simple matching tasks:

sed -n 's/.*committed=\([0-9]*\).*/\1/p' input_file
perreal
  • 94,503
  • 21
  • 155
  • 181
4
$ awk -F'[=)]' '{print $3}' file
1728469KB
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
1

You can try this:

str="Internal (reserved=1728469KB, committed=1728469KB)"
echo $str | awk '{print $3}' | cut -d "=" -f2 | rev | cut -c4- | rev
Soumendra Mishra
  • 3,483
  • 1
  • 12
  • 38