简体   繁体   中英

Awk remove specific characters

I have command, which gives me output from telnet. Full output from telnet It looks like this:

telnet myserver.com 1234

Server, Name=MyServer, Age=123, Ver=1.23, ..., ..., ...

This command should filter just the number after Age - "Age= 123 " which I want to filter:

echo "\n" | nc myserver.com 1234 | (awk -F "=" '{print $3}')

Instead of 123 it gives me this output:

123, Ver

Is there a way how to get just number after Age= ?

It's just bad awk filtering parameter, but I tried some other ways with awk but this gave me almost best result... Thank you for any help.

Edit: I forgot, number after Age= is dynamic +1 every day...

I'm not sure about the echo "\\n" part but I think that this should do what you want:

nc myserver.com 1234 | awk -F "," '{ split($3, a, /=/); print a[2] }'

Instead of splitting into fields on the = , I've done so on the , . The third field is then split into the array a on the = and the second half is printed.

I also removed the ( ) around the invocation of awk, which was creating a subshell unnecessarily.

If you're confident about the response never varying containing = or , in other places, you could simplify the awk expression further:

awk -F'[=,]' '{ print $5 }'

The bracket expression allows fields to be split on either = or , , making the part you're interested in the fifth field.

You can run the awk command twice.

awk -F "=" '{print $3}'|awk -F "," '{print $1}'

You can also use the cut command:

cut -d "=" -f 3|cut -d "," -f 1
echo "\n" | nc myserver.com 1234 | awk -F "," '{print substr($3,6)}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM