SED practice, some SED common methods

1. Copy the /etc/rc.d/rc.local file to the /tmp directory, and copy the
Add # at the beginning of lines that start with at least one blank character.
sed -ri’s/^ +/#/g’ rc.local
2. Copy /boot/grub2/grub.conf to the /tmp directory and delete the blank characters at the beginning of the line in the /tmp/grub.conf file.
#sed -r’s/(^ +)(.*)//g’ /tmp/grub.conf ++++ +> Only delete blank characters in lines with blank characters at the beginning
sed -nr’s/^ +//pg’ /tmp/grub.conf +++++> Delete all blank characters at the beginning of the line
3. Delete the # beginning with # in the /tmp/rc.local t file, and # And white space characters in lines followed by at least one white space character.
sed -r -e’s/#(.*)//g’ -e’s/ +(.*)//g’ rc.local < /em>
4. Add # to the beginning of the first three lines in the /tmp/grub.conf file.
sed -ri ‘1,3 s/(.*)/#/g’ rc.local
5. Set all enabled=0 or gpgcheck in the /etc/yum.repos.d/CentOS-Media.repo (just find one) file =0
The last 0 is changed to 1
sed -nr ‘/enabled|gpgcheck/ s/.*=(.*)/1/gp’ local.repo
6, every 4 Perform a backup of the /etc directory once every hour, back up to the /backup directory, and the saved directory name is like etc-201504020202.
* */4 * * * cp /etc/* /backup/etc-$(date +%Y%m%d%H%M )
7, every week 2, 4, 6 backup /var/log/messages file to /backup/messages_logs/ directory, The saved file name is like messages-20150402.
crontab -e

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

8. Get all the information starting with S in the current system /proc/meminfo file to the /stats/memory.txt file every two hours every day.
crontab -e
* */2 * * * sed- nr’/^S.*/p’ /proc/meminfo> /stats/memory.txt
9. Work day During the time, the echo “howdy” is executed every two hours.
* * * * 1-5 echo “howday”
10. Create directory /tmp./testdir-current date and time
mkdir testdir-$(date +%F-% H:%M:%S)
12. Display the user name of the user on the even-numbered line in the /etc/passwd file.
sed -n ‘2~2p’ /etc/passwd

1. Copy the /etc/rc.d/rc.local file to the /tmp directory, and copy the
Add # at the beginning of lines that start with at least one blank character.

sed -ri’s/^ +/#/g’ rc.local

2. Copy /boot/grub2/grub.conf to the /tmp directory and delete the blank characters at the beginning of the line in the /tmp/grub.conf file.
#sed -r’s/(^ +)(.*)//g’ /tmp/grub.conf ++++ +> Only delete blank characters in lines with blank characters at the beginning
sed -nr’s/^ +//pg’ /tmp/grub.conf +++++> Delete all blank characters at the beginning of the line
3. Delete the # beginning with # in the /tmp/rc.local t file, and # And white space characters in lines followed by at least one white space character.
sed -r -e’s/#(.*)//g’ -e’s/ +(.*)//g’ rc.local < /em>

4. Add # to the beginning of the first three lines in the /tmp/grub.conf file.

sed -ri ‘1,3 s/(.*)/#/g’ rc.local

5. Set all enabled=0 or gpgcheck in the /etc/yum.repos.d/CentOS-Media.repo (just find one) file =0
The last 0 is changed to 1
sed -nr ‘/enabled|gpgcheck/ s/.*=(.*)/1/gp’ local.repo

6, every 4 Perform a backup of the /etc directory once every hour, back up to the /backup directory, and the saved directory name is like etc-201504020202.
* */4 * * * cp /etc/* /backup/etc-$(date +%Y%m%d%H%M )

7, every week 2, 4, 6 backup /var/log/messages file to /backup/messages_logs/ directory, The saved file name is like messages-20150402.
crontab -e

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

8. Get all the information starting with S in the current system /proc/meminfo file to the /stats/memory.txt file every two hours every day.
crontab -e
* */2 * * * sed- nr’/^S.*/p’ /proc/meminfo> /stats/memory.txt

9. Work day During the time, the echo “howdy” is executed every two hours.
* * * * 1-5 echo “howday”

10. Create directory /tmp./testdir-current date and time

mkdir testdir-$(date +%F-% H:%M:%S)

12. Display the user name of the user on the even-numbered line in the /etc/passwd file.
sed -n ‘2~2p‘ /etc/passwd

Leave a Comment

Your email address will not be published.