Linux Getting Started sed command case

Source: Internet
Author: User

Linux Getting Started sed command case

Actual combat:

1, delete the/etc/grub.conf file in the beginning of the blank character;

Sed-r ' [email protected]^[[:spapce:]][email protected]@g '/etc/grub.conf

2. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5;

Sed ' [email protected]\ (id:\) [0-9]\ (: initdefault:\) @\15\[email protected] '/etc/inittab

3, delete the blank line in the/etc/inittab file;

Sed '/^[[:space:]]\?$/d '/etc/inittab

4. Delete the # number at the beginning of the/etc/inittab file;

Sed ' [email protected]^#@@g '/etc/inittab

5, delete the beginning of a file # and the following white space characters, but requires the # number must be followed by a blank character;

Sed-r ' [email protected]^#[[:space:]][email protected]@g '/etc/inittab

6. Delete white space characters at the beginning of the line of the # class followed by white space characters in a file and #

Sed-r ' [Email protected]^[[:space:]]+#@@g '/etc/inittab

7. Take out a directory name for a file path;

echo "/etc/rc.d/" | Sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\[email protected] '


Case TWO:

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

Sed '/^[[:space:]]/d '/etc/grub.conf

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

Sed ' s/^#[[:space:]]\+//g '/etc/grub.conf

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

Sed ' s/^\ (. *\)/#\1/g '

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

Sed ' s/^[^#].*/#&/g '/etc/fstab

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

#取目录 [[email protected] ~]# echo "/sdf/fdg/" | Sed ' s/^\ (\/.*\/\) [^\/]\+\/\?$/\1/g '/sdf/#取基名 [[email protected] ~]# echo "/sdf/fdg/" | Sed ' s/^\ (\/.*\/\) \ ([^\/]\+\) \/\?$/\2/g ' fdg# take base name [[email protected] ~]# echo "/et/" | Sed ' s/^\ (\/.*\) \<//g ' et/
#万能方法: #取目录名 [[email protected] ~]# echo "/et.sdf/df/ggsdfd/" | Sed ' s/^\ (\/.*\) \<\ (. *\) \+\/\?/\2/g ' ggsdfd/#取基名 [[email protected] ~]# echo "/et.sdf/df/ggsdfd/" | Sed ' s/^\ (\/.*\) \<\ (. *\) \+\/\?/\1/g '/et.sdf/df/

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

#简易型: Sed-n '/inet[^0-9]/p ' | Sed-n ' s/.*addr:\ (. *\) [[: Space:]]\+b.*/\1/p '
#复杂型: [[email protected] ~]# Ifconfig | Sed-n ' {/inet[^0-9]/p;} ' | Sed ' {s/^.*r:\ (\<[0-9\.) \+\>\) [[: Space:]]\+\ (B...t:\ (\<[0-9\.] \+\>\) [[: Space:]]\+\] \? M.. K:\ (\<[0-9\.) \+\>\)/\1/g;/^[[:space:]]\?$/d} ' 192.168.20.13310.1.249.71127.0.0.1# Note: \1 for IP \3 for broadcast address \4 for subnet mask

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] packages]# ls./* | Sed-n ' s/^.*\.\ (. *\) \.RPM$/\1/GP ' | Sort | Uniq-c 4 i686 919 Noarch 2283 x86_64


This article is from the "Excalibur Trunks-kun" blog, please be sure to keep this source http://mengzhaofu.blog.51cto.com/10085198/1836492

Linux Getting Started sed command case

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.