Home > Enterprise >  REGEX using gawk command
REGEX using gawk command

Time:11-29

In Linux, I'm running the command

pmap -x $PID | tail -n 1

This gives me a line like the following:

total kB         168194812  870692  852296

I'm trying to extract the 2nd number (rss) for use. I found this example that works in regex101.com:

/[^\d]*[\d] [\s] ([\d] )/

However, when I try to run it against my line of text I don't get any print output:

echo "total kB         168194812  870692  852296" | gawk 'match($0, /[^\d]*[\d] [\s] ([\d] )/, a) {print a[1]}'

I'm expecting it to print

870692

CodePudding user response:

Like this:

$ pmap -x $PID | gawk 'match($0, /[^0-9]*[0-9] \s ([0-9] )/, a) {print a[1]}'
870692

The expression \d is specific Perl/PCRE compatible regex. Some languages like Python use this too.

You can simplify to:

awk '{print $4}'

Using :

grep -oP '\d (?=\s \d $)'

CodePudding user response:

What about just displaying the 4th field with

awk '{print $4}'

With your example

echo "total kB         168194812  870692  852296" | awk '{print $4}'

returns

870692

CodePudding user response:

If you want to use awk you can match digits with [0-9] and the negated version [^0-9]

As you output a single line with tail -n 1, using gnu awk you could also set the row separator to 1 or more digits, and print the row terminator when the row number is 2.

echo "total kB         168194812  870692  852296" | 
awk -v RS='[0-9] ' 'NR == 2 {print RT}'

Output

870692
  • Related