Source of demand: The default IP link show command output will output the network card (including the virtual network card) name, MAC address, port status, and other information, but also mixed with some useless information, this time the string operation is to this information in the network card name, MAC address, port status taken out.
The knowledge involved includes the use of commonly used string processing tools for awk, sed, cut, etc., paste by column, printf formatted output, and so on.
For example, the original output information is:
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M01/59/6D/wKiom1TSx9vigKKoAAMLjctETp8043.jpg" height= "304" />
Now we have to extract all the information in the red box, but the difficulty is that the default output of the IP link show command prints the MAC address on the next line, causing the normal awk operation not to be extracted by column.
But there are always ways:
Method 1: Using awk, with the If else judgment, the rows at the beginning of the number are the required columns, not the columns required to output the rows at the beginning of the numbers, and the desired results are obtained by printing them together.
IP link Show | awk ' {if ($0~/^[0-9]+:/) printf ("%-15s%-15s", $2,$9), else print-$} '
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M02/59/6A/wKioL1TSyMfTDvd6AAG91er_wBQ786.jpg" height= "303" />
Method 2: Use awk or cut to extract the required columns from each row, and then merge the columns into columns (but this is tedious, and formatting the output is not easy and requires the final format output).
interface_name= ' ip link show | grep ^[1-90-9] | cut -d ":" -f 2 ' mac_address= ' ip link show | grep -v ^[1-90-9] | cut -d " " -f 6 state= ' ip link show | grep ^[1-90-9] | cut -d ' ' -f 9 ' temp= ' paste < (echo "$INTERFACE _name") < (echo "$MAC _address") ' Paste < (echo "$temp") < (echo "$STATE") temp2= ' paste < (echo "$temp") < (echo "$STATE") ' echo ' $temp 2 " | awk ' {printf ("%-15s%-20s%-15s\n ", $1,$2,$3)} '
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M00/59/6A/wKioL1TSyMfxZzN1AAJJVeroVF8698.jpg" height= "371" />
Method 3: Format the IP link show command output to print the MAC address on the same line.
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M00/59/6D/wKiom1TSx9zCqJ5fAAU-t8DKujM264.jpg" height= "282" />
Use the-o parameter of IP to control the output on the same line. The parameter is equal to "-oneline", meaning is the same line, explain as shown, have already considered well, this kind of design is very regrettable unceasingly!
The output each record is on a single line, and replacing line feeds with the ' \′character. This was convenient when you want to count records with WC (1) or to grep (1) the output. "
--end--
This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1611783
Linux Bash Shell string extraction, merge by column, and formatted output