Displaying IP address on eth0 interface
Displaying IP address on eth0 interface
Awk
ifconfig eth0 | awk '/inet addr/{split($2,a,":"); print a[2]}'
split function in the above awk command splits the second column based on the delimiter
: and stores the splitted value into an associative array a. So a[2] holds the value of the second part.
sed
ifconfig eth0 | sed -n '/inet addr/s/.*inet addr: *\([^[:space:]]\+\).*/\1/p'
In basic sed ,
\(...\) called capturing group which is used to capture the characters. We could refer those captured characters through back-referencing. \([^[:space:]]\+\) captures any character but not space one or more times.
grep
ifconfig eth0 | grep -oP 'inet addr:\K\S+'
\K discards the previously matched characters from printing at the final and \S+ matches one or more non-space characters.
Perl
ifconfig eth0 | perl -lane 'print $1 if /inet addr:(\S+)/'
One or more non-space characters which are next to the
inet addr: string are captured and finally we print those captured characters only.
Nhận xét
Đăng nhận xét