In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Sed is a stream editor that processes one line of content at a time. When processing, the currently processed line is stored in a temporary buffer, called "pattern space" (pattern space), and then the contents of the buffer are processed with the sed command, and when the processing is completed, the contents of the buffer are sent to the screen. Then read down the line and execute the next loop. If there is no special command such as'D', the pattern space is cleared between the two loops, but not the reserved space. This is repeated until the end of the file. The contents of the file have not changed unless you use redirect to store the output.
The function of sed: it is mainly used to edit one or more files automatically, simplify the repeated operation of files, write conversion programs, etc.
Basic usage of sed
Sed [option]... 'script' inputfile...
Sed [option].. Script this script is the script of sed itself.
Common options:
-n does not output mode space content to the screen, that is, it does not print automatically
-e multipoint editing
-f / PATH/SCRIPT_FILE reads the edit script from the specified file
-r supports the use of extended regular expressions
-i.bak backup file and edit it in place: modify it directly
Sed-i.bak (self-defined)'/ ^ root/aadmin'; actually modifies the file and backs up the original file
Script:
Address command: the address deals with that part of the text, and how to deal with those lines is handled according to the command
Address demarcation:
(1) No address: process the full text
(2) single address:
#: specified line, $: last line
/ pattern/: every line that can be matched by the pattern here
(3) address range:
#, #
#, + #
/ pat1/,/pat2/: only shows the middle part
For example: sed-n'/ ^ ftp/,/ ^ sa/p' / etc/passwd
#, / pat1/
(4): step by step
1x 2 odd lines
Seq 10 | sed-n '1cm 2p'
2an even number of lines
Seq 10 | sed-n '2cm 2p'
Sed-n '2p' / etc/passwd: print only the second line
The sed command is not only for text, but also for the standard output of some commands as input.
For example: ifconfig eth0 | sed-n '2p'
Edit commands:
D delete the rows that match the pattern space and immediately enable the next loop (the second line of sed '2d' / etc/passwd is not displayed)
P prints the contents of the current mode space, appended to the default output (it will print once after adding p)
A [] text appends text after the specified line, and supports the use of\ nto implement multiple line append
Seq 10 | sed 'axxx': appends xxx to each line
You can also add a newline character: seq 10 | sed 'axxx\ nttt\ nzzz'
You can also add a newline character: seq 10 | sed '2axxx\ nttt\ nzzz'
Append the second line
I [] text inserts text before the line: the content after adding the slash representation is the added content
C [] text substitution behavior single-line or multi-line text
W / path/file saves lines that match the pattern to the specified file
After r / path/file reads the text of the specified file to the matching line in the pattern space
Sed-n'/ ^ UUID/r / etc/issue' / etc/fstab: read the contents of the file
= print line numbers for lines in mode space
! Matching row inversion processing in pattern space
Sed''/ etc/passwd: nothing is equivalent to showing the file.
Modify the configuration file in selinux
Sed-I'/ ^ SELINUX=enforcing/cSELINUX=disabled' / etc/selinux/config
Disable SElinux
Sed-I ^ C / ^ SELINUX = / cSELINUX=disabled' / etc/selinux/config
Scandinavia: find replacement, delimiter is specified by yourself, and can be used as a substitute for swatches.
Replace tag: G, global replace; I, ignore case
Advanced editing command
H: overwrite the contents of the schema space to the holding space
H: append the contents of the schema space to the holding space
G: overwrite the contents of the hold space to the pattern space
G: append the contents of the hold space to the pattern space
X: interchange the contents of the pattern space and the hold space
N: overrides the next line of the matched row read to the pattern space
N: append reads the next line of the matched row to the pattern space
D: delete rows in schema space
D: delete rows in multiline mode space
Forcibly reference variables in sed statements
'' var_name''': uses three 'quotation marks to forcibly reference variable values in sed statements
Example: IP=IP
[root@centos7 test] # ifconfig ens33 | sed-nr 's#.inet (.) net.*#'''$IP'''\ 1roomp'
IP 192.168.0.100
Example:
Seq 1 10 | sed-n'nscape p': take even lines
Seq 1 10 | sed'nscape d': fetch odd lines
Seq 1-10 | sed'1: displaying in reverse order
Seq 1 10 | sed'$! d': take out the last line
Seq 1 10 | sed'/ ^ $/ d * * G': delete blank lines in the file, and then add a blank line to all non-blank lines
Seq 1 10 | sed'$! NittAccordance D': displays the last two lines
Seq 1 10 | sed'G': add a blank line after each line of the original file
Sed activity:
Exercise: count and sort the number of occurrences of each word in the / etc/init.d/functions file (using the grep and sed commands)
Sed-nr's # [^ [: alpha:]] + #\ nSecretgp' / etc/rc.d/init.d/functions | sort | uniq-c | sort-nr
Grep-Eo "[[: alpha:]] +" / etc/rc.d/init.d/functions | sort | uniq-c | sort-nr
Exercise: remove the IP address of the specified network card
Ifconfig eth0 | sed-rn '2s/ (^ .inet) (.) (netmask.*$) /\ 2Universe p'
Ifconfig eth0 | sed-nr '2s/.t (.) net.*/\ 1Universe p'
Exercise: echo / etc/sysconfig/network/ takes out the directory name and base name
Directory name: echo / etc/sysconfig/network/ | sed-nr's # (. /) ([^ /].) /? $#\ 1roomp'
Base name: echo / etc/sysconfig/network/ | sed-nr's # (. /) ([^ /].) /? $#\ 2roomp'
Exercise: merge the n and nail1 lines of the text file into one line
N is an odd line: seq 1 10 | sed-n'n places #\ nroomp'
N is an even-numbered line: seq 1 10 | sed-n'1d tincture Nittina s#\ nroomroomp'
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.