Skip to main content

Posts

Showing posts from December, 2015

Unix shell: print out a few lines before and after the finding of an output line

when I search an IP from the ifconfig output, I want to search for which NIC has this IP, so I use this shell command to achieve so: the command below print out totally 10 lines, 4 lines before and 5 lines after ifconfig -a|grep -A 5 -B 5 10.250.7.40 linenum=`ifconfig -a|awk '{if ($0~ /10.250.7.40/)   print NR;}'`;ifconfig -a|head -`expr $linenum + 5`|tail -10 linenum=`ifconfig -a|grep -n 10.250.7.40|awk -F":" '{print $1}'`;ifconfig -a|head -`expr $linenum + 5`|tail -10 the command below print out 5 lines before and 10 lines after the matching line. ifconfig -a|grep -C 5 -10 10.250.7.40 the command below print out 5 lines before and 5 lines after the matching line. ifconfig -a|grep -C 5  10.250.7.40