Sed and vim exercises

Source: Internet
Author: User

1. Delete all whitespace characters from the beginning of lines in the/etc/grub2.conf file that begin with whitespace

[[email protected] ~]# sed-r ' s/^[[:space:]]+//g '/etc/grub2.cfg # # do don't EDIT this file## It's automatically generated by Grub2-mkconfig using templates# from/etc/grub.d and Settings from/etc/default/grub#### Begin/etc/grub.d/00_header # # #set Pager=1if [-S $prefix/grubenv]; Thenload_env ...

2. Delete all # and white space characters at the beginning of the line beginning with #, followed by at least one white-space character, in the/etc/fstab file

[[email protected] ~]# sed -r  ' [email protected]^#[[:space:]]+\[email  Protected]@g '  /etc/fstab ## /etc/fstabcreated by anaconda on wed aug   3 06:08:10 2016#Accessible filesystems, by reference, are  maintained under  '/dev/disk ' See man pages fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for more info#UUID=1aa7af5a-15a0-4cf2-a11d-8304d67c7093 /                         xfs     defaults         0 0UUID=2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot                    xfs      defaults        0 0UUID=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994 /testdir                 xfs      Defaults        0 0uuid=1d6f94c4-7eb4-430c-b143-b63b498c1c98  swap                     swap    defaults        0  0

3. Add # At the beginning of each line of/root/install.log

[[Email protected] ~]# sed ' s/^/#/g '/etc/fstab

##### /etc/fstab## created by anaconda on wed aug  3  06:08:10 2016#### accessible filesystems, by reference, are maintained  under  '/dev/disk ' ## see man pages fstab (5),  findfs (8),  mount (8)   And/or blkid (8)  for more info## #UUID =1aa7af5a-15a0-4cf2-a11d-8304d67c7093 /                         xfs     defaults         0 0#UUID=2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot                    xfs      defaults        0 0#uuid=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994  /testdir    &nbSp;           xfs     defaults         0 0#uuid=1d6f94c4-7eb4-430c-b143-b63b498c1c98 swap                      swap    defaults        0 0

4. Add # # At the beginning of the line in the/etc/fstab file that does not begin with #

[[email protected] ~]# sed  ' s/^[^#]/#&/g '  /etc/fstab ## /etc/fstab#  Created by anaconda on Wed Aug  3 06:08:10 2016##  accessible filesystems, by reference, are maintained under  '/dev/disk ' #  see man pages fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for  more info# #UUID =1aa7af5a-15a0-4cf2-a11d-8304d67c7093 /                        xfs      defaults        0 0#uuid= 2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot                    xfs     defaults         0 0#uuid=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994 /testdir                 xfs     defaults         0 0#UUID=1d6f94c4-7eb4-430c-b143-b63b498c1c98 swap                     swap     defaults        0 0

Note: The top carriage return line does not add #, that is, there is no match return line

[[email protected] ~]# sed   '/^#/p;s/^[^#]/#&/pg;/^#/d;s/^/#/'   /etc/ Fstab ### /etc/fstab# created by anaconda on wed aug  3  06:08:10 2016## accessible filesystems, by reference, are maintained  under  '/dev/disk ' #  # see man pages fstab (5),  findfs (8),  Mount (8)  and/or blkid (8)  for more info# #UUID =1aa7af5a-15a0-4cf2-a11d-8304d67c7093  /                        xfs     defaults         0 0#UUID=2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot                    xfs      defaults        0 0#uuid=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994 /testdir                 xfs      defaults        0 0#uuid= 1d6f94c4-7eb4-430c-b143-b63b498c1c98 swap                     swap    defaults         0 0
[[email protected] ~]# sed -r  ' s/^ ([^#]|$)/#&/'  /etc/fstab ### / etc/fstab# created by anaconda on wed aug  3 06:08:10 2016##  Accessible filesystems, by reference, are maintained under  '/dev/disk ' #  # see man pages fstab (5),  findfs (8),  mount (8)  and/or  Blkid (8)  for more info# #UUID =1aa7af5a-15a0-4cf2-a11d-8304d67c7093 /                         xfs     defaults        0 0# uuid=2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot                    xfs     defaults         0 0#uuid=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994 /testdir                 xfs     defaults         0 0#uuid=1d6f94c4-7eb4-430c-b143-b63b498c1c98 swap                      swap    defaults        0 0

5, processing/etc/fstab path, use sed command to take out its directory name and base name

[Email protected] ~]# echo "/etc/init.d/functions" |sed ' s/.*\<//' functions
[Email protected] ~]# echo "/etc/init.d/functions" |sed-r ' [email protected][^/][email protected]@g '/etc/init.d/
[Email protected] ~]# echo "/etc/init.d/functions" |sed-r ' [email protected] (. *)/([^\]+.?) [Email protected]\[email protected] '/etc/init.d[[email protected] ~]# echo "/etc/init.d/functions" |sed-r ' [email Protected] (. *)/([^\]+.?) [Email protected]\[email protected] ' functions

6. Use SED to remove the IPv4 address of the ifconfig command

[Email protected] ~]# ifconfig |sed-r '/^[^]/!d; n;s/^ ([^]+). *\n[^0-9]+ ([0-9.] +). */\1 \2/' eno16777736:10.1.70.102lo:127.0.0.1virbr0:192.168.122.1

7. Statistics of all RPM files in the package directory on the CentOS installation CD. Number of repetitions of the second-to-last field

[[email protected] ~]# ls/mnt/packages/|sed-r ' s/.*\b ([^.] +)\. [^.] +$/\1/' |sort|uniq-c i686 2938 noarch 1 TRANS 4069 x86_64
[Email protected] ~]# ls/mnt/packages/|rev|cut-d-f2|rev|sort|uniq-c i686 2938 noarch 1 TRANS 4069 x8 6_64

1. How do I set the tab indent to 4 characters?

: Set ts=4

2. Copy the/etc/rc.d/init.d/functions file to the/tmp directory, replace the/etc/sysconfig/init in the/tmp/functions file with the/var/log

:%[email protected]/etc/sysconfig/[email Protected]/var/[email protected]

3. Delete the # number at the beginning of all lines in the/tmp/functions file that begin with # and have at least one blank character after #

:%s/^#//g


Sed and vim exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.