Using SED (Stream EDitor)

SED (Stream EDitor) — streaming text editor and programming language.
An excellent tool to convert outgoing text data in any convenient form.

For example, several times wrote a table of online mac-addresses, which were taken from the switch, were checked with billing and were output with such data as address, ID, login, etc ..
For example, a script that receives SNMP macros from the L3 of the HP 5800 switch and saves them in a text document in a column, thanks to the SED, unnecessary data is deleted, and spaces are replaced with a colon:

#!/bin/bash
rm /var/www/mac.txt
snmpwalk -v 2c -c community 192.168.1.50 .1.3.6.1.2.1.17.4.3 -O v|sed -e "s/.*Hex-STRING: //g" -e "s/ /:/g" -e "s/.$//" -e "/INTEGER/d" > /var/www/mac.txt

Here are a few more examples of using SED.
Output only values after Hex-STRING:

sed -e 's/.*Hex-STRING: //g'

Colon whitespace replacement:

sed -e 's/ /:/g'

Delete the last line:

sed -e sed 's/.$//'

Delete the last character:

sed -e 's/.$//'

Removing a string containing INTEGER:

sed -e '/INTEGER/d'

Removing lines in file.txt where ABCD occurs:

sed -i '/^ABCD/ d' file.txt

Example of deleting the third line in the file file.txt and deleting 4 lines starting from line 7:

sed -i '3,1d' file.txt
sed -i '7,4d' file.txt

Enumerate the lines in file.txt:

sed = file.txt | sed 'N;s/\n/\t/'

Outputting only rows from 5-10:

sed -n 5,10p file.txt

The built-in help can be obtained by the command:

man sed

Leave a Reply