sed practice, some of the commonly used methods sed

1. Copy the /etc/rc.d/rc.local file to the / tmp directory, the /tmp/rc.sysinit file
to a blank row at least the beginning of a line or character #.
sed -ri 's/^ +/#/g'  rc.local
2, /boot/grub2/grub.conf copy to the / tmp directory, delete the whitespace characters of /tmp/grub.conf file.
whitespace characters #sed -r 's / (^ + ) (. *) / \ 2 / g' /tmp/grub.conf +++++> delete only the beginning of the line there is a blank character
sed -nr 's / ^ + // pg '/tmp/grub.conf +++++> delete all blank characters beginning of a line
3, beginning with # /tmp/rc.local t delete files, and followed by at least one blank character # characters and blank lines.
 sed -r -e 's / # ( . *) / \ 1 / g' -e 's / + (. *) / \ 1 / g' rc.local
4, for the first three rows of the file /tmp/grub.conf beginning of the line by #.
sed -ri '1,3 s/(.*)/#\1/g' rc.local
5, the /etc/yum.repos.d/CentOS-Media.repo (stuffing a) all files enabled = 0 or gpgcheck = 0 of
the last modification to 0. 1
Sed -NR '/ Enabled | gpgcheck / S /.*=(.*)/1/gp 'local.repo
6, once every 4 hours / etc directory backup, to backup / backup directory, the directory name is stored in the form etc-201504020202.
* * / 4 * * * cp / etc / * / backup / etc - $ (date +% Y% m% d% H% M)
7, 2,4,6 weekly backup / var / log / messages file to / backup / messages_logs / directory, the file name of the form stored messages-20150402.
    crontab -e

* * * * 2,4,6 cp /var/log/messages  /backup/messages_logs/messages-$(date +%Y%m%d)

8, all per day beginning with S /stats/memory.txt file information to the current system taken every two hours / proc / meminfo file in.
     -e the crontab
  * * / 2 * * * -NR Sed '/^S.*/p' / proc / meminfo> /stats/memory.txt
9, during working hours on weekdays, once every two hours to perform echo "howdy".
 * * * * 1-5 echo "howday "
10, create a directory /tmp./testdir- current date and time
mkdir testdir-$(date +%F-%H:%M:%S)
12, the display / etc / passwd file in the user's username even rows.
sed -n '2 ~ 2p' / etc / passwd

Guess you like

Origin www.cnblogs.com/chenxi123/p/11627166.html
sed
Recommended