In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
For row processing tools, sed is absolutely the first choice.
Sed processes text as follows:
Sed processes one line at a time, reads one line first, removes the trailing newline, stores it in pattern space, and executes editing commands. After processing, unless you add the-n parameter, print out the current pattern space and print the newline character that has been removed. Give the pattern space content to hold space, leave pattern space blank, read on to the next line, and process the next line.
This makes people feel very abstract, and sedsed's tools are designed to demonstrate the sed process. Sedsed is easy to install and use, and it is necessary to install sedsed tools in order to better understand sed.
Wget http://aurelio.net/sedsed/sedsed-1.0touch / usr/local/bin/sedsedcat sedsed-1.0 > > / usr/local/bin/sedsedchmod 755 / usr/local/bin/sedsed
The usage is as follows
Usage: sedsed OPTION [- e sedscript] [- f sedscriptfile] [inputfile] OPTIONS:-f,-- file add file contents to the commands to be parsed-e,-- expression add the script to the commands to be parsed-n,-- quiet suppress automatic printing of pattern space--silent alias to-- quiet-d,-- debug debug the sedscript-- hide hide some debug info (options: PATT,HOLD COMM)-- color shows debug output in colors (default: ON)-- nocolor no colors on debug output--dump-debug dumps to screen the debugged sed script--emu emulates GNU sed (INCOMPLETE)-- emudebug emulates GNU sed debugging the sed script (INCOMPLETE)-I,-- indent script beautifier, prints indented andone-command-per-line output do STDOUT--prefix indent prefix string (default: 4 spaces)-t,-- tokenize script tokenizer, prints extensivecommand by command information-H -- the htmlize converts sed script to a colorful HTML page-f option is the same as the-f option of sed-d to open debug Where-- hide means to hide the specified content For example,-- hide=hold represents the contents of the hidden reserved space buffer-- I-- indent formats complex sed scripts into more humane scripts.
Let's start with a simple example:
[root@localhost ~] # cat 1.txt
Linux
Centos
Redhat
Linux
Ubuntu
Fedora
To replace linux with debian, here are three processing methods: sed processing, sedsed-d and sedsed-d-- hide
[root@localhost ~] # sed's root@localhost root@localhost 1.txtPATT:linux$HOLD:$COMM:s/linux/debian/gPATT:debian$HOLD:$debianPATT:centos$HOLD:$COMM:s/linux/debian/gPATT:centos$HOLD:$centosPATT:redhat$HOLD:$COMM:s/linux/debian/gPATT:redhat$HOLD:$redhatPATT:linux$HOLD:$COMM:s/linux/debian/gPATT:debian$HOLD:$debianPATT:ubuntu$HOLD:$COMM:s/linux/debian/gPATT:ubuntu$HOLD 1.txtPATT:linux$HOLD:$COMM:s/linux/debian/gPATT:debian$HOLD:$debianPATT:centos$HOLD:$COMM:s/linux/debian/gPATT:centos$HOLD:$centosPATT:redhat$HOLD:$COMM:s/linux/debian/gPATT:redhat$HOLD:$redhatPATT:linux$HOLD:$COMM:s/linux/debian/gPATT:debian$HOLD:$debianPATT:ubuntu$HOLD:$COMM:s/linux/debian/gPATT:ubuntu$HOLD g '1.txtdebiancentosredhatdebianubuntufedora [root@localhost ~] # Linux-d's : $ubuntuPATT:fedora$HOLD:$COMM:s/linux/debian/gPATT:fedora$HOLD:$ fedora [root @ localhost ~] # sedsed-d-- hide=hold 's 1.txtPATT:linux$COMM:s/linux/debian/gPATT:debian$debianPATT:centos$COMM:s/linux/debian/gPATT:centos$centosPATT:redhat$COMM:s/linux/debian/gPATT:redhat$redhatPATT:linux$COMM:s/linux/debian/gPATT:debian$debianPATT:ubuntu$COMM:s/linux/debian/gPATT:ubuntu$ubuntuPATT:fedora$COMM:s/linux/debian/gPATT:fedora$fedora LinuxCompact Debianand g'Unix
Where:
PATT:sedsed outputs the contents of the display mode space buffer
COMM: displays the command being executed
HOLD: displays the contents of the hold sapce buffer
Addition, deletion, modification and search of sed
Sed is used for delete operations: d and D
The command "d" deletes the contents of the pattern space, then reads in a new line, and the sed script executes from scratch.
The difference of the command "D" is that it deletes part of the pattern space until the first newline character is embedded, but does not read the new line, and the script goes back to starting to process the rest.
Look at an example.
[root@localhost ~] # cat 2.txtThis line is followed by 1 blank line.This line is followed by 2 blank line.This line is followed by 3 blank line.This line is followed by 4 blank line.This is the end. [root@localhost ~] # sed'/ ^ $/ {Nash / ^\ nCompare D} '2.txtThis line is followed by 1 blank line.This line is followed by 2 blank line.This line is followed by 3 blank line.This line is followed by 4 blank line.This is the end. [root@localhost ~] # sed' / ^ $/ {N / ^\ nCompact d} '2.txtThis line is followed by 1 blank line.This line is followed by 2 blank line.This line is followed by 3 blank line.This line is followed by 4 blank line.This is the end.
Open debug with sedsed to see the execution process / / the content behind is my comment.
[root@localhost ~] # sedsed-d'/ ^ $/ {N / ^\ nCompact D} '2.txtPATT:This line is followed by 1 blank line.$ / / pattern space reads the first line of content HOLD:$ / / hold space begins to empty COMM:/ ^ $/ {/ / the command being executed, determines whether it is a blank line, obviously not, all the following commands are not executed The result of the execution is sent to the screen and the result is sent to the hold space.PATT:This line is followed by 1 blank line.$HOLD:$This line is followed by 1 blank line.PATT:$ / / pattern space to read the second line HOLD:$ / / hold space start or empty COMM:/ ^ $/ {/ / the command being executed to determine whether it is a blank line, obviously All execute the following commands COMM:N / / execute N read the next line into the pattern space PATT:\ nThis line is followed by 2 blank line.$HOLD:$ / / at this time the hold space is still empty COMM:/ ^\ nspace / D / / A pair of pattern spaces continue to execute the following commands: if it is a blank line, execute the D command, obviously not, all are not executed. PATT:\ nThis line is followed by 2 blank line.$ / / pattern space content HOLD:$ / / hold space content, still empty. COMM:} PATT:\ nThis line is followed by 2 blank line.$HOLD:$This line is followed by 2 blank line. / / because the execution condition is not met, continue to read the next line. PATT:$ / / blank line, meet the execution conditions, execute the command HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ n$HOLD:$COMM:/ ^\ ncommands / D / / meet the conditions for executing the D command Delete a blank line (there are now two blank lines) PATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ nThis line is followed by 3 blank line.$HOLD:$COMM:/ ^\ nblank / DPATT:\ nThis line is followed by 3 blank line.$HOLD:$COMM:} PATT:\ nThis line is followed by 3 blank line.$HOLD:$This line is followed by 3 blank line.PATT:$\\ blank line, meet the command execution conditions, execute the command Read the next line. HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ n$HOLD:$COMM:/ ^\ ncommands / D\\ satisfy the execution D command, delete a blank line. PATT:$\\ blank line, meet the command execution conditions, execute the command, read the next line. HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ n$HOLD:$COMM:/ ^\ ncommands / D\ satisfy the execution D command Delete a blank line. PATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ nThis line is followed by 4 blank line.$HOLD:$COMM:/ ^\ nregions / DPATT:\ nThis line is followed by 4 blank line.$HOLD:$COMM:} PATT:\ nThis line is followed by 4 blank line.$HOLD:$This line is followed by 4 blank line.PATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ n$HOLD:$COMM:/ ^\ n lines / DPATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT: \ n$HOLD:$COMM:/ ^\ ngirls / DPATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ n$HOLD:$COMM:/ ^\ nlegs / DPATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ nThis is the end.$HOLD:$COMM:/ ^\ nlegs / DPATT:\ nThis is the end.$HOLD:$COMM:} PATT:\ nThis is the end.$HOLD:$This is the end. [root@localhost ~] # sedsed-d'/ ^ $/ {N / ^\ nCompact d} '2.txtPATT:This line is followed by 1 blank line.$HOLD:$COMM:/ ^ $/ {PATT:This line is followed by 1 blank line.$HOLD:$This line is followed by 1 blank line.PATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ nThis line is followed by 2 blank line.$HOLD:$COMM:/ ^\ nsalary / dPATT:\ nThis line is followed by 2 blank line.$HOLD:$COMM:} PATT:\ nThis line is followed by 2 blank line.$HOLD:$This line is Followed by 2 blank line.PATT:$\\ blank line Meet the conditions for executing the command, execute the command, and read the next line. HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ n$HOLD:$COMM:/ ^\ ncommands / d\\ meet the conditions for executing the d command At this point, both blank lines in the mode space are deleted. PATT: This line is followed by 3 blank line.$HOLD:$COMM:/ ^ $/ {PATT:This line is followed by 3 blank line.$HOLD:$This line is followed by 3 blank line.PATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ n$HOLD:$COMM:/ ^\ n spaces / dPATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ nThis line is followed by 4 blank line.$HOLD:$COMM:/ ^\ n spaces / dPATT:\ NThis line is followed by 4 blank line.$HOLD:$COMM:} PATT:\ nThis line is followed by 4 blank line.$HOLD:$This line is followed by 4 blank line.PATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ n$HOLD:$COMM:/ ^\ n$HOLD:$COMM:/ / dPATT:$HOLD:$COMM:/ ^ $/ {COMM:NPATT:\ n$HOLD:$COMM:/ ^ / dPATT:This is the end.$HOLD:$COMM:/ ^ $/ {PATT:This is the end.$HOLD:$This is the end.
In general, it is deleted with d, such as:
Delete the blank line: sed'/ ^\ s delete filenamesed / ^ [[: space:]] * $/ d' filename use the line label to delete sed 'n1d' filename delete line N1 sed' n1mn2d' filename delete the content between lines N1 and N2 (N1
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.