Sed simple application

Source: Internet
Author: User
Tags processing text

I. SED application processing Text Tool Sedstream editor, row editor Sed is a stream editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, 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 empty the reserved space. This keeps repeating until the end of the file. The file content does not change unless you use redirection to store the output. Function: Mainly used to automatically edit one or more files, simplify the repeated operation of the file, write the conversion program, such as SED tool usage:

sed [option] ... ' Script ' Inputfile ...

Common options:

-N does not output mode space content to the screen, that is, does not print automatically

-e Multi-point editing

-f/path/script_file reading the edit script from the specified file

-R supports the use of extended regular expressions

-i.bak backup files and edit them in place

Script
' Address command

Address delimitation:

(1) Not to address: The full text of the processing

(2) Single address:
"#: Specified line, $: last line

/pattern/: Each row to which the pattern can be matched

(3) Address range:

“#,#”

"#,+#"

/pat1/,/pat2/

"#,/pat1/"

(4) ~: Step forward

Odd lines

2~2 even rows

Edit command:

D Delete the line that matches the pattern space and immediately enable the next round of loops

P Print the current mode space content, appended to the default output

A []text appends text to the specified line, enabling multi-line append using \ n

I []text insert text in front of a row

c []text replaces a line of behavior or multiple lines of text

W/path/file save pattern matching rows to the specified file

R/path/file reads the text from the specified file to the line in the pattern space

= Print line numbers for lines in pattern space

! Matching row-fetching anti-processing in pattern space

s///find replacement, support the use of other separators, [email protected]@@,s###

Replace tag:

G in-line global substitution

P shows the row that replaced the successful

W/path/file save a successful row to a file

Second, experiment 1, delete all whitespace characters in the CENTOS7 system/etc/grub2.cfg file beginning with blank lines
sed -nr "s/^[[:space:]]//p" /etc/grub2.cfg -r  支持扩展正则表达式s///  替换

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 -r ‘/^#[[:space:]]+/d‘ /etc/fstab

3. Add # At the beginning of each line of the CENTOS6 system/root/install.log
sed ‘s/^/#/‘ /root/install.log
4. Add # to the beginning of the line in the/etc/fstab file that does not begin with #
sed ‘[email protected]^[^#]@#&@‘ /etc/fstabs//&/  后项引用

5, processing/etc/fstab path, use sed command to take out its directory name and base name
echo "/etc/sysconfig/network-scripts/"|sed -r ‘[email protected](^/.*/)([^/]+/?)@\[email protected]‘取基名 echo "/etc/sysconfig/network-scripts/"|sed -r ‘[email protected](^/.*/)([^/]+/?)@\[email protected]‘取目录名 s/()()/\1/ 后项引用 s/()()/\2/
6. Take advantage of SED to remove the IPv4 address of the Ifconfig command (different version names, note when writing the path.)
ifconfig|sed -n ‘2p‘|sed ‘s/^.*r://‘ |sed ‘s/ .*//‘ ifconfig|sed -n ‘2p‘|sed ‘s/^[[:space:]].*r://‘|sed ‘s/[[:space:]].*//‘ ifconfig|sed -n ‘2p‘|sed -e ‘s/^[[:space:]].*r://‘ -e ‘s/[[:space:]].*//‘ ifconfig|sed -n -e ‘2s/^[[:space:]].*r://‘ -e ‘2s/[[:space:]].*//p‘
ifconfig ens33|sed -n ‘2p‘|sed -r ‘[email protected]^[[:space:]].*t[[:space:]]@@‘ 取broadcast地址 ifconfig ens33|sed -n ‘2p‘|sed -r ‘[email protected]^[[:space:]].*inet[[:space:]]@@‘|sed -r ‘[email protected][[:space:]].*@@‘ ifconfig ens33|sed -n ‘2p‘|sed -r -e ‘[email protected]^[[:space:]].*inet[[:space:]]@@‘ -e ‘[email protected][[:space:]].*@@‘ ifconfig|sed -n ‘2p‘|sed -r ‘[email protected]*inet[[:space:]](.*)[[:space:]]net.*@\[email protected]‘
7. Statistics of all RPM files in the package directory on the CentOS installation CD-ROM. The number of repetitions of the second-to-last field (different version paths, note the path to write).
ls /misc/cd/Packages/|grep "rpm$"|sed -r ‘[email protected]*\.(.*)\[email protected]\[email protected]‘|sort|uniq -c ls /misc/cd/Packages/|grep "rpm$"|sed -e ‘[email protected][email protected]@‘ -e ‘[email protected]*\[email protected]@‘|sort|uniq -c ls /misc/cd/Packages/|rev|cut -d. -f2|sort -nr|rev|uniq -c|sort -nr|head -n3
8. Count the number of occurrences of each word in the/etc/init.d/functions file and sort it (using grep and sed two methods respectively)
9. Merge N and n+1 lines of text file into one line, n is odd line
nl /etc/passwd|sed -n ‘N;s/\n//p‘

END

Sed simple application

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.