Reputation: 3
Is there a way to make this command more simple (ie. not using awk twice):
awk '/cpu MHz/ {print $4}' < /proc/cpuinfo | awk -F'.' 'NR==1 {print $1}'
?
Also, is there a way to avoid using cut command here:
ip addr show dev eth0 | awk '$1=="inet" {print $2}' | cut -f1 -d '/'
?
Thanks.
Upvotes: 0
Views: 289
Reputation: 41460
If the goal is to show IP address on your interface, then this is not a good option:
ip addr show dev eth0
What if interface has another name, or you have more than one interface and eth1
is the one connected to internet?
Us this instead:
ip route get 8.8.8.8 | awk '/8.8.8.8/ {print $NF}'
192.168.0.33
To store it in an variable:
IP=$(ip route get 8.8.8.8 | awk '/8.8.8.8/ {print $NF}')
It uses ip route
command to see how to get to an public IP, so will work correct for any name of interface.
Upvotes: 1
Reputation: 290105
First one is like this:
awk '/cpu MHz/ {print $4}' < /proc/cpuinfo | awk -F'.' 'NR==1 {print $1}'
Considering you have a string like this:
cpu MHz : 800.000
cpu MHz : 800.000
cpu MHz : 800.000
cpu MHz : 800.000
And you want the integer part of the number just for first line, you can do:
$ awk -F"[ .]" '/cpu MHz/ {print $(NF-1); exit}' /proc/cpuinfo
800
That is, to match the line, print the penultimate field and then exit to avoid extra processing.
As per comments, you also want to get the 2nd matching line in a different command. For this, you can do:
awk -F"[ .]" '/cpu MHz/ {if (f) {print $(NF-1); exit}; f=1}' /proc/cpuinfo
What it does is to use a flag. On the first match, the flag is activated, so on next one it prints the value and exits.
Second one is like this:
ip addr show dev eth0 | awk '$1=="inet" {print $2}' | cut -f1 -d '/'
What you can do is to split the second field in slices based on /
and print the first one:
$ ip addr show dev eth0 | awk '$1=="inet" {split($2, a, "/"); print a[1]}'
192.168.123.123
Upvotes: 1