Parsing data from ifconfig with awk or sed?
use grep
:
ifconfig | grep -oP '(?<=RX bytes:)[0-9]*'
use awk
:
ifconfig | awk -F: '/RX bytes/{print $2+0}'
Parse ifconfig to get only my IP address using Bash
Well, after hours of struggling I finally got it right:
ifconfig en1 | awk '{ print $2}' | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"
That last part I had missing is just grep a pattern of IP addresses from my list.
Parse interfaces from ifconfig
I'll add this one to the pile. Add the quotes in the first sed, then just join the lines with paste.
ifconfig -a | sed -n 's/^\([^ ]\+\).*/"\1"/p' | paste -sd ","
This is better than tr since there's no trailing comma.
Better yet, since ifconfig -a output can't really be counted on to stay consistent, check /sys/class/net
ls /sys/class/net | sed -e 's/^\(.*\)$/"\1"/' | paste -sd ","
display only network interface and ip address using ifconfig and awk
You can use ip route show
instead to get the information you want. Filter with this command for example:
ip r show|grep " src "|cut -d " " -f 3,12
outputs something like:
eth0 192.168.1.114
awk to get IP info
Here is a working version based on Kents verson:
ifconfig -a|awk -v RS="" '{for(i=1;i<=NF;i++){
if($i=="HWaddr") mac=$(i+1)
else if($i~/addr:[0-9]/) {split($i,a,":");ip=a[2]}
else if($i~/Mask/) {split($i,a,":");mask=a[2]}}
if(ip!="127.0.0.1")print $1,mac,ip,bcast,mask}'
eth0 00:18:71:6a:f0:45 192.168.1.30 255.255.255.0
I have skipped the Broad Cast address, since its always the last IP in the segment and normally not needed.
How to extract TX packets from ifconfig
Assuming your kernel is Linux, you're best off asking the kernel to give you the number you want itself:
adapter=eth0
tx_packets=$(</sys/class/net/"$adapter"/tx_packets) || {
echo "Could not find $adapter" >&2; exit 1
}
echo "Adapter $adapter sent $tx_packets packets"
That way you aren't reliable on a tool that's likely to go away in the future. (ifconfig
on Linux has been unmaintained for over a decade now in favor of the iproute2 suite). Moreover, sysfs is where ifconfig
is getting the numbers from itself -- so you're cutting out quite a range of middlemen by going there directly.
As for your attempt at a script, see BashFAQ #50 for a full, detailed description of why it failed and how to reuse code without putting that code in strings. Personally, if I were going to parse ifconfig
's output in that way, I might write:
#!/bin/bash
# ^^^^- pipefail not guaranteed to work with /bin/sh, so use bash here!
set -o pipefail # ensure that pipeline fails if ifconfig does
adapter="eth0"
tx_packets=$(ifconfig "$adapter" | awk '/TX packets/ {print $2}' | cut -d ":" -f2) || exit
echo "Adapter $adapter sent $tx_packets packets"
However, I wouldn't advise parsing ifconfig
's output at all.
Note that this output format is not standardized across operating systems, so you can't rely on the popularity of that name (for OS-specific interface configuration tools) to imply that output format will comply with any particular standard.
How to use pipe to parse output of a command
You can achieve this using awk as shown on this site. I've posted an excerpt here which produces your desired result:
ifconfig | awk '/dr:/{gsub(/.*:/,"",$2);print$2}'
Use awk output as variable for string comparison
This generally means that you've got nonprintable characters -- like syntax highlighting, or DOS newlines -- in your variable.
One easy way this can happen is if you've got your grep
instance set to always insert color codes, as with --color=always
rather than the default (safer) --color=auto
.
To track this down, compare the output of the following commands:
printf '%s' "$en1_status" | xxd
printf '%s' inactive | xxd
You can also moot grep
's configuration by letting awk
do the searching:
en1_status=$(ifconfig en1 | awk '/status: / { print $2 }')
Related Topics
Jna Link Issue While Starting Cassandra Rhel 6.5
How to Tell If Running in a Linux Console Versus an Ssh Session
Getting Meteor 0.9.2 Build to Work Osx -> Linux
Compile Linux Kernel (2.6) Module Including Non Kernel Headers
Can Any One Say How to Install the Slack in Ubuntu14.04 and While Trying This Was the Error
What Is the Meaning of !#:* !#:1- in a Bash Command
How to Wait for a Keystroke Interrupt with a Syscall on Linux
Unwanted Line Break Using Echo and Cat
Qmake .Pro File Not Parsed Correctly to Generate Ld_Library_Path
Find Value from One CSV in Another One (Like Vlookup) in Bash (Linux)
How to Find the Particular Text Stored in the File "Data.Txt" and It Occurs Only Once
Set Environment Variables for Non-Interactive Shell
How to Capture All the Commands Typed in Unix/Linux by Any User
Get Last Parameter on Shell Script