Linux Foundation-----SED Tool Application Questions

Source: Internet
Author: User

Tools for working with the text sed
? Stream Editor, line editors
? SED is a stream editor that processes a single line of content at a time. When processing, stores the currently processed rows in a temporary
In the buffer, called the pattern space, followed by the SED command to handle the
Content, and after processing is done, the contents of the buffer are sent to the screen. Then read the downstream line and execute the next loop.
If you do not have a special command such as ' D ', it will empty the pattern space between the two loops, but will not clear
Empty reserved space. This keeps repeating until the end of the file. The contents of the file do not change unless you use the heavy
Directed storage output.
? function: Mainly used to automatically edit one or more files, simplify the repeated operation of the file, write the conversion program, etc.
Reference

1. Delete all whitespace characters in the CENTOS7 system/etc/grub2.cfg file that begin with whitespace at the beginning of the line
2. Delete all lines in the/etc/fstab file beginning with #, followed by at least one whitespace character #
and whitespace characters
3. Add # to the beginning of the line in the/etc/fstab file that does not begin with #
4, processing/etc/fstab path, use sed command to take out its directory name and base name
5. Use sed to remove the IPv4 address of the ifconfig command
6. Statistics of all RPM files in the package directory on the CentOS installation CD-ROM. Separate the second from the bottom
Number of repetitions of the field
7. Count the number of occurrences of each word in the/etc/init.d/functions file, and sort (with grep and
Sed two ways to achieve each)
.

1.cat/etc/grub2.cfg |sed-r ' s/^[[:space:]]+//'

2.cat/etc/fstab |sed-r ' s/(^#[[:space:]?) ‘

3.cat/etc/fstab |sed-r ' s/^ ([^#].) /#\1/'

4.1 FileName: echo "/etc/fstab" | Sed ' [Email protected][^/][email protected]@ '

4.2 Base name: Echo "/etc/fstab" | Sed ' [email protected]
/([^/]+] [email protected]\[email protected] '


5.*ifconfig ens33|sed-n ' 2p ' |sed ' s/. inet (.) Netmas. /\1/' *

There are many methods for this problem, as follows:
Ifconfig ens33| Sed-r ' 2!d; [Email protected] (.
inet) (.) (NET.) @\[email protected] '
Ifconfig ens33|sed-n ' 2p ' |sed ' s/. inet//' |sed ' S/netmask.

6.ls/misc/cd/packages/|grep "rpm$" |sed-r ' s/. . (.). rpm$/\1/' |sort |uniq-c

7.1sed Method: Cat/etc/init.d/functions | Sed-r ' [email protected][^[:alpha:]][email protected]\[email protected] ' |sort |uniq-c|sort-n

7.2grep Method: Cat/etc/init.d/functions | Grep-o-E "\<[[:alpha:]]+\>" |sort|uniq-c |sort-n

7.3tr Method: Cat/etc/init.d/functions | Tr-c ' [: Alpha:] ' \ n ' |sort|uniq-c|sort-n

These three methods, we can see, tr and sed method concept is consistent, all adopt the idea of complement set, the character that is not letter all convert to newline, then carry on the statistic, and Grep-o method, divide each word, and carry on separate branch, follow up to carry on statistic again.
Three methods, grep and tr/sed get the answer is not the same, there is a certain method of a loophole.
This problem I have not thought of the solution at present, I hope the great God pointed out
20180412

Linux Foundation-----SED Tool Application Questions

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.