AWK regular expression print inside brackets

Asked

Viewed 246 times

4

look, inside my.txt file has the following description:

Flags: X - disabled, E - established

 0 E name="peer1_cymru" instance=default remote-address=38.xx.xx.xx
     remote-as=65555 tcp-md5-key="WUf4f8" nexthop-choice=default
     multihop=yes route-reflect=no hold-time=3m ttl=default
     in-filter=CYMRU-IN out-filter=CYMRU-OUT address-families=ip,ipv6
     update-source=ether1 default-originate=never remove-private-as=no
     as-override=no passive=no use-bfd=no remote-id=xx.xx.xx.xx
     local-address=xx.xx.xx.xx uptime=3h32m11s prefix-count=90668
     updates-sent=0 updates-received=90668 withdrawn-sent=0
     withdrawn-received=0 remote-hold-time=3h used-hold-time=3m
     used-keepalive-time=1m refresh-capability=yes as4-capability=yes
 state=established

I need it on the linux terminal using awk which is what I know to only get the prefix-Count=90668 item. It would need the output of the command to be equal: [90668] How to proceed?

2 answers

4

Try using this with the input you receive:

'awk 'match($0, /prefix-count=(.*)/) {
print substr($0, RSTART, RLENGTH)

Explanation:

'awk 'match($0, - Starts awk and starts the Regex part with the space 0 of the memory, where the input will be inserted.

/prefix-count=(.*)/ - This states that Regex will capture anything after the sequence prefix-Count= until the end of the line.

print substr($0, RSTART, RLENGTH) - This prints what Regex returns from start to finish match

  • Obs: If the output you want includes the brackets in the RAW form of the output, just use this: awk 'match($0, /prefix-Count=(. *)/) { print substr([$0], RSTART, RLENGTH)

1

You can use this command:

awk -F = '/90668$/{print $NF}' arquivo

The option -F specifies that "=" is a field separator! awk searches the file for the default value /90668$/ the character $ (final) server to indicate that this value is at the end of the line! print $NF is to print the last field ! could also have been so!

awk -F = '/8$/{print $NF}'

90668 file that would have the same result! in the case of this issue itself

  • Can you explain what this command does?

  • the -F option and to specify that "=" is a field separator! awk searches the file for the default value /90668$/ the character "$" (end) server to indicate that value is at the end of the line! print $NF and to print the last field ! could also have been like this! awk -F = '/8$/{print $NF}' file 90668 q would have the same result! in the case of this question itself!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.