Sed

Commands

  • Print the first line: sed -n '1p' anaconda-ks.cfg
  • Print the first 5 lines: sed -n '1,5p' anaconda-ks.cfg
  • Print the last line: sed -n '$p' anaconda-ks.cfg
  • Print all lines except the first 5 lines: sed -n '1,5!p' anaconda-ks.cfg
  • Pring all lines with repo: sed -n '/repo/p' anaconda-ks.cfg
  • Delete a particular line: sed -i 14d ~/.ssh/known_hosts
  • Replace only on the first 30 lines: sed -i '1,30s/nagios/nagiosadmin/g' /usr/local/nagios/etc/nrpe.cfg
  • Change the string nagios to nagiosadmin on the lines with nrpe: sed -i /nrpe/s/nagiosadmin/nagios/ /usr/local/nagios/etc/nrpe.cfg
  • Add a string to beginning of each line: sed -i 's/^/pvcreate \/dev\//g' emc_devices.lst

  • Enable all epel repos: sed -i "s/enabled=0/enabled=1/g" /etc/yum.repos.d/epel.repo

  • Replace the value for GRUB_DEFAULT to 1: sed -i.bak '/^GRUB_DEFAULT=/s/=.*/=1/' /etc/default/grub or sed -i.bak 's/^GRUB_DEFAULT=.*/GRUB_DEFAULT=1/' /etc/default/grub
  • Append something to the end of line (add -i to edit in place if all looks good): sed -e '/check_netstat/s/$/ \$ARG3\$/' /usr/local/nagios/etc/nrpe.cfg
  • Backup the file and append something to the end of the line: sed -i.bak '/^allowed_hosts=/s/$/,192.168.1.27/' /usr/local/nagios/etc/nrpe.cfg
  • Change a setting: sed -i 's/^dont_blame_nrpe=0/dont_blame_nrpe=1/g' /usr/local/nagios/etc/nrpe.cfg
  • Delete all lines that start with "#": sed -i '/^#/d' /usr/local/nagios/etc/nrpe.cfg
  • Delete all blank lines: sed -i '/^$/d' /usr/local/nagios/etc/nrpe.cfg