In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
How to learn SED, I believe that many inexperienced people do not know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The test files used below can be found at https://github.com/mylxsw/sed-demo.
Overview
SED, whose English full name is Stream EDitor, is a simple and powerful text parsing and conversion tool developed by Lee E. McMahon of Bell Labs from 1973 to 1974. Today, it runs on all major operating systems.
McMahon created a generic line editor that eventually became SED. Many of the syntax and features of SED draw on the ed editor. Since the beginning of the design, it has supported regular expressions, and SED can accept input from files similar to pipes, or from standard input streams.
SED is developed and maintained by the Free Software Foundation (FSF) and distributed with GNU/Linux, so it is often referred to as GNU SED. The syntax of SED may seem mysterious to beginners, but once you have mastered its syntax, you can solve very complex tasks with only a few lines of code, which is the charm of SED.
Typical uses of SED
SED has a wide range of uses, such as:
Text substitution
Optional output text file
Start editing somewhere in the text file
No interactive editing of text files, etc.
Work flow
In this chapter, we will explore how SED works, and to become an SED expert, you need to know its internal implementation. SED follows a simple workflow: read, execute, and display, which is described in the following figure:
Read: SED reads a line from the input stream (file, pipe, or standard input) and stores it in an internal buffer called pattern buffer
Execution: by default, all SED commands are executed sequentially in the mode space, and SED commands will be executed sequentially on all lines unless the address of the line is specified
Display: sends the modified content to the output stream. After sending the data, the schema space will be emptied.
The above process will be repeated before all the contents of the file are processed.
A few points to pay attention to
The pattern space (pattern buffer) is an active buffer that saves the text to be checked when the sed editor executes the command
By default, all SED commands are executed in the schema space, so the input file does not change
There is another buffer called hold space (hold buffer), which can be used to temporarily save some rows when dealing with certain rows in the pattern space. At the end of each loop, SED removes the contents of the schema space, but the contents of the buffer are persisted during all loops. The SED command cannot be executed directly in the buffer, so SED allows data to switch between hold space and schema space
In the initial case, keep both the space and pattern space buffers empty
If no input file is provided, SED will receive requests from standard input
If no address range is provided, SED will operate on all lines by default
Example
Let's create a text file called quote.txt, which contains a famous quote by the famous writer Paulo Coelho
$vi quote.txt There is only one thing that makes a dream impossible to achieve: the fear of failure. -Paulo Coelho, The Alchemist
To understand the workflow of SED, we first use SED to display the contents of the quote.txt file, which is similar to the cat command
$sed 'quote.txt There is only one thing that makes a dream impossible to achieve: the fear of failure. -Paulo Coelho, The Alchemist
In the above example, quote.txt is the name of the file entered, and the two single quotes are the SED commands to be executed.
First, SED will read a line from the quote.txt file and store it in its schema space, and then execute the SED command in that buffer. Here, no SED command is provided, so there is no action to be performed on the buffer. * it deletes the content in the schema space and prints it to standard output. It's a simple process, right?
In the following example, SED accepts input from the standard input stream
$sed''
When the above command is executed, the following results will be produced
There is only one thing that makes a dream impossible to achieve: the fear of failure. There is only one thing that makes a dream impossible to achieve: the fear of failure.
In this case, the content of the * line is entered through the keyboard, and the second line is the content of the SED output.
Exit from the SED session and use the key combination ctrl-D (^ D)
Basic grammar
This chapter describes the basic commands in SED and how to use them on the command line. SED can be called in two ways:
Sed [- n] [- e] 'command (s)' files sed [- n]-f scriptfile files
* use single quotation marks to specify the command to be executed on the command line, and the second method specifies the script file that contains the SED command. Of course, these two methods can also be used at the same time, and SED provides a number of parameters to control this behavior.
Let's see how to specify multiple SED commands. SED provides the delete command to delete some lines. Here, let's delete the * * lines, the second line and the fifth line:
First, use the cat command to display the contents of the file
$cat books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
Now, use SED to remove the specified line, and to delete three lines, we use the-e option to specify three separate commands
$sed-e '1d'-e' 2d'-e '5d' books.txt 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. Tolkien, 4326) A Game of Thrones, George R. Martin, 864
We can also write multiple SED commands in a text file, and then use that file as an argument to the SED command. SED can execute each command in the file on the contents of the pattern space. The following example describes the second use of SED.
First, create a text file that contains the SED command. For ease of understanding, we use the same SED command as before
$echo-e "1D\ n2d\ n5d" > commands.txt $cat commands.txt 1d 2d 5d
Next, construct a SED command to perform the operation
$sed-f commands.txt books.txt 3) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4326) A Game of Thrones, George R. R. Martin, 864
Standard option
SED supports the following standard options:
-n by default, content in mode space is printed to standard output after processing is completed, which is used to prevent this behavior
$sed-n''quote.txt
-e specifies the command to be executed. With this parameter, we can specify multiple commands so that we can print each line twice:
$sed-e'- e 'p' quote.txt There is only one thing that makes a dream impossible to achieve: the fear of failure. There is only one thing that makes a dream impossible to achieve: the fear of failure.
. Paulo Coelho, The Alchemist
. Paulo Coelho, The Alchemist
-f specifies the script file that contains the command to be executed
$echo "p" > commands $sed-n-f commands quote.txt There is only one thing that makes a dream impossible to achieve: the fear of failure.
. Paulo Coelho, The Alchemist
GNU option
These options are defined by the GNU specification and may not be supported for some versions of SED.
-n,-- quiet,-- slient: same as the standard-n option
-e script,--expression=script: same as the standard-e option
-f script-file,-- file=script-file: same as the standard-f option
-- follow-symlinks: if this option is provided, SED will follow the link when the edited file is a symbolic link
-I [SUFFIX],-- in-place [= SUFFIX]: this option is used to edit the current file. If SUFFIX is provided, the original file will be backed up, otherwise the original file will be overwritten.
-l N,-- line-lenght=N: this option sets the length of the line to N characters
Posix: this option disables all GNU extensions
-rmam r, r
-u,-- unbuffered: when this option is specified, SED will load the least amount of data from the input file and flush it out to the output buffer more frequently. This option is useful when editing the output of the tail-f command and you don't want to wait for the output.
By default, SED splits each line with newline characters. If this option is provided, it will split lines using NULL characters.
Cycle
Like other programming languages, SED provides loops and branching statements that control the flow of execution.
The loop in SED is somewhat similar to the goto statement. SED can jump to a line based on the label to continue execution. In SED, we can define the following tags:
: label: start: end: up
In the above example, we created four tags.
To jump to the specified tag, use the b command followed by the tag signature, and if you ignore the tag signature, SED will jump to the end of the SED file.
The b tag is used to jump unconditionally to the specified label.
To better understand the loops and branches in SED, let's create a text file called books2.txt that contains some book title and author information. In the following example, the title and author of the book are merged, separated by commas. Then search for all lines that match "Paulo" and add-at the beginning of the line if there is a match, otherwise jump to the Print tag and print out the line.
$cat books2.txt A Storm of Swords George R. R. Martin The Two Towers J. R. R. Tolkien The Alchemist Paulo Coelho The Fellowship of the Ring J. R. R. Tolkien The Pilgrimage Paulo Coelho A Game of Thrones George R. R. Martin $sed-n'hten X s / R Tolkien, / / Print p 'books2.txt A Storm of Swords, George R. R. Martin The Two Towers, J. R. Tolkien-The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. Tolkien-The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin
At first glance, the above code is very mysterious. Let's disassemble it step by step.
* the line is h * * n * * H The commands x, remember the hold space we mentioned above? * h refers to overwriting the contents of the current mode space to the hold space, n is used to read the next line in advance, and overwrites this line in the current mode space, H appends the contents of the current mode space to the hold space, and the x of * * is used to swap the pattern space and keep the contents of the space. So this means that you read two lines at a time and put them in the pattern space and give them to the following command for processing.
Then there is sAccord, which is used to replace the newline character in the above two lines with a comma.
The third command jumps to the Print tag when there is a mismatch, otherwise continue with the fourth command
: Print is just a signature, while p is a print command
To improve readability, each command occupies one line, of course, you can also put all the commands on one line.
$sed-n'hten / Print;p' books2.txt / Print; / Print;p' books2.txt
Refer to the official manual sed, section 3.6Less Frequently-Used Commands of a stream editor for the command of hmag Hmmex.
Branch
Use the t command to create a branch. The t command jumps to the tab only if the current setting is successful.
The t command is executed only if the previous replace (s) command is executed successfully.
Let's look at some of the examples in the previous chapter. Unlike before, this time we will print four hyphens "-", compared to the previous one.
Loop / Paulo/s/ ^ /-/-/! t Loop p 'books2.txt A Storm of Swords, George R. Martin The Two Towers, J. R. Tolkien-The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. Tolkien-The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin
In the above example, the first two lines work in the same way as in the previous section. The third line defines a Loop tag, then matches the line where "Paulo" exists, and adds a-if it exists. Here's what we're going to focus on:
The line of /-/! t Loop first checks whether the above four are satisfied, and if not, jump to Loop to continue with the third line, so that you can continue to append -, * if the line change meets the previous four-before continuing to execute.
To improve readability, we put each SED command on a separate line, or we can use it on the same line:
Sed-n'hwittern Tiptsmx; s /\ nGrease, /;: Loop;/Paulo/s/ ^ /-/; /-/! t Loop; p 'books.txt
Pattern space and hold space
Pattern space
The most basic operation for any file is to output its contents, and to achieve this, you can use the print command in SED to print out the contents of the schema space.
First create a file that contains the line number, title, author, and page number, which we will use in this article, and you can create any other file, but here we will create a file that contains the following
$vi books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho,288 6) A Game of Thrones, George R. R. Martin, 864
Execute p command
$sed 'p' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 3522) The Two Towers, J. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1973) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4324) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho 288 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 6) A Game of Thrones, George R. R. Martin, 864
You may wonder why each line is displayed twice.
Do you remember the workflow of SED? By default, SED will output the contents of the mode space, in addition, our command contains the output command p, so each line is printed twice. But don't worry, SED provides the-n parameter to disable the automatic output of each row of the pattern space.
$sed-n'p 'books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 2886) A Game of Thrones, George R. R. Martin, 864
Row addressing
By default, commands used in SED act on all lines of text data. If you only want to apply the command to a specific line or certain lines, you need to use the line addressing feature.
There are two forms of row addressing in SED:
Interline space in digital form
Filter lines in text mode
Both forms use the same syntax format
[address] command
Digital line addressing
In the following example, SED will only operate on line 3
$sed-n '3p' books.txt 3) The Alchemist, Paulo Coelho, 197
Of course, we can also have SED output some lines. Use commas in SED to separate the range of output line numbers, for example, the following code outputs 2-5 lines
$sed-n'2 5 p 'books.txt 2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288
The special character $represents the * * line of the file and the * * line of the output file
$sed-n'$p 'books.txt 6) A Game of Thrones, George R. R. Martin, 864
You can also use $to specify the address range of the output, and the following command outputs the third line to the * line
$sed-n'3 The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho,288 6) A Game of Thrones, George R. R. Martin, 864
SED also provides two other operators for the specified address range. * * is the plus (+) operator, which can be used with the comma (,) operator, for example, M, + n will print the next n line starting from line M. The following example will output the following four lines at the beginning of the second line
$sed-n'2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
We can also use the wavy operator (~) to specify the address range, which is in the form of masking N, which tells SED that every N line at the beginning of the M line should be processed. For example, 50 minutes 5 matches line numbers 50, 5, 5, 5, 60, 65, etc., let's output only odd lines in the file.
$sed-n '1mm 2 p' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 3) The Alchemist, Paulo Coelho, 1975) The Pilgrimage, Paulo Coelho, 288
The following code outputs only the even lines in the file
$sed-n'2p 'books.txt 2) The Two Towers, J. R. R. Tolkien, 3524) The Fellowship of the Ring, J. R. R. Tolkien, 4326) A Game of Thrones, George R. Martin, 864
Note that if you are using the sed command that comes with the Mac system, the ~ and + operators may not be supported. You can use brew install gnu-sed-- with-default-names to reinstall GNU-SED.
Use text mode filter
The SED editor allows you to specify a text mode to filter out the lines that the command wants to act on. The format is as follows:
/ pattern/command
The specified pattern must be sealed with a forward slash. The sed editor applies the command to the line that contains the specified text pattern.
In the following example, all books whose authors are Paulo Coelho will be output.
$sed-n'/ Paulo/ p 'books.txt 3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288
Pattern matching can also be used in conjunction with numeric addressing. In the following example, the output starts from * matches to Alchemist until line 5.
$sed-n'/ Alchemist/, 5p 'books.txt 3) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288
Use the comma (,) operator to specify that multiple matching patterns are matched. The following example will output all lines between Two and Pilgrimage
$sed-n'/ Two/, / Pilgrimage/ p 'books.txt 2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288
When using a text mode filter, similar to numeric line addressing, you can use the plus operator +, which outputs a few lines from the current matching position. The following example outputs the next four lines from the location where the Two appears.
$sed-n'/ Two/, + 4 p 'books.txt 2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
Keep the space
When working with some rows in the mode space, you can save some rows by keeping the space coming. There are five commands that can be used to maintain space.
Command description h copy pattern space to hold space H attach pattern space to hold space g copy hold space to pattern space G attach hold space to pattern space x swap the contents of pattern space and hold space
With regard to preserving space, there are no examples here, and we have already explained its use when we explained the following command in the recycling section.
$sed-n'hten / Print;p' books2.txt / Print; / Print;p' books2.txt
Basic command
This chapter will explain some commonly used SED commands, including commands such as DELETE,WRITE,APPEND,CHANGE,INSERT,TRANSLATE,QUIT,READ,EXECUTE.
Delete command d
The delete command format is as follows
[address1 [, address2]] d
Address1 and address2 are start and end addresses, which can be line numbers or string matching patterns, both of which are optional.
As can be seen from the name of the command, the delete command is used to perform delete operations, and because SED is a line-based editor, we say that the command is used to delete lines. Note that this command only removes the line in the pattern space so that the line is not sent to the output stream, but the original content is not changed.
$sed 'd'books.txt
Why didn't you output anything? By default, SED will delete each line, which is why the command does not output anything in standard output.
The following command removes only the fourth line
[jerry] $sed '4d' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1975) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. Martin, 864
SED also accepts address ranges separated by commas (,). We can construct an address range to remove lines N1 to N2. For example, the following command will delete lines 2-4
$sed '2,4d' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. Martin, 864
The address range of SED is not limited to numbers. We can also specify pattern matching as the address. The following example removes all books whose authors are Paulo Coelho.
$sed'/ Paulo Coelho/d' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 3524) The Fellowship of the Ring, J. R. R. Tolkien, 4326) A Game of Thrones, George R. R. Martin, 864
I remove all lines that start with Storm and Fellowship
$sed'/ Storm/,/Fellowship/d' books.txt 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
File write command w
SED provides the write command to write the contents of the schema space to a file, similar to the delete command, the following is the syntax of the write command
[address1 [, address2]] w file
W specifies the write command, and file refers to the file name in which the contents of the file are stored. Be careful when using the file operator, which is automatically created when a file name is provided but the file does not exist, and overwrites the contents of the original file if it already exists.
The following SED command creates a copy of the file books.txt, with only one space between w and file
$sed-n'w books.bak' books.txt
The above command creates a file called books.bak to verify that the contents of the two files are the same
$diff books.txt books.bak $echo $?
Once the above code is executed, you will get the following output
0
As smart as you may have thought, this is what cp commands do! Indeed, the cp command does the same thing, but SED is a mature tool that allows you to copy only certain lines from a file to a new file. The following code stores odd lines in the file to another file.
$sed-n'2w junk.txt' books.txt $cat junk.txt 2) The Two Towers, J. R. R. Tolkien, 3524) The Fellowship of the Ring, J. R. R. Tolkien, 4326) A Game of Thrones, George R. Martin, 864
Suppose you want to store all the books of independent authors into separate files. If you do it manually, it must be very boring and unskilled, but with SED, you have a smarter way to do it.
$sed-n-e'/ Martin/ w Martin.txt'-e'/ Paulo/ w Paulo.txt'-e'/ Tolkien/ w Tolkien.txt' books.txt $cat Martin.txt 1) A Storm of Swords, George R. Martin, 1216 6) A Game of Thrones, George R. Martin, 864$ cat Paulo.txt 3) The Alchemist, Paulo Coelho, 1975) The Pilgrimage, Paulo Coelho, 288$ cat Tolkien.txt 2) The Two Towers, J. R. Tolkien, 3524) The Fellowship of the Ring J. R. R. Tolkien, 432
Append command a
Text append command syntax:
[address] a\ Append text
Add a new book after the fourth line:
$sed'4a 7) Adultry, Paulo Coelho, 234' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197) The Fellowship of the Ring, J. R. Tolkien, 4327) Adultry, Paulo Coelho, 2345) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
In the command section, 4 refers to the line number, an is the append command, and the rest is the text to be appended.
Insert a line of text at the end of the file, using $as the address
$sed'$a 7) Adultry, Paulo Coelho, 234' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. Martin, 864 7) Adultry, Paulo Coelho, 234
In addition to line numbers, we can also use text mode to specify an address, for example, appending text after lines that match The Alchemist
$sed'/ The Alchemist/ a 7) Adultry, Paulo Coelho, 234' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197) Adultry, Paulo Coelho, 2344) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. Martin, 864
Line replacement command c
SED provides change and replace commands through c, which help us replace existing lines with new text. When the address range of lines is provided, all lines are replaced as a set of single-line text. Here is the syntax of the command
[address1 [, address2]] c\ Replace text
For example, replace the third behavior in the text with new content
$sed'3 c 3) Adultry, Paulo Coelho, 324 'books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3523) Adultry, Paulo Coelho, 3244) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. Martin, 864
SED also accepts mode as the address
$sed'/ The Alchemist/ c 3) Adultry, Paulo Coelho, 324 'books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 3523) Adultry, Paulo Coelho, 324) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. Martin, 864
Multi-line substitution is also supported, and the following command replaces lines 4-6 with a single line
$sed'4, 6 c 4) Adultry, Paulo Coelho, 324 'books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197 4) Adultry, Paulo Coelho, 324
Insert command I
The insert command is similar to the append command, except that the insert command inserts a new line before the matching position.
[address] I\ Insert text
The following command inserts a new line before the fourth line
$sed'4 I 7) Adultry, Paulo Coelho, 324 'books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197) Adultry, Paulo Coelho, 324 4) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
Conversion command y
The Translate command y is the only sed editor command that can handle a single character. The format of the conversion command is as follows
[address] y/inchars/outchars/
The conversion command maps the inchars and outchars values one-to-one. The * characters in inchars are converted to * characters in outchars, and the second character is converted to the second character in outchars. This mapping process continues until the specified character is processed. If the length of inchars and outchars is different, the sed editor generates an error message.
$echo "151520" | sed'yUnix 151520 Universe'IV IV XX
Output hidden character command l
Can you tell whether words are separated by spaces or tab by direct observation? Obviously not, but SED can do it for you. Use the l command (lowercase of the letter L) to display hidden characters (such as t or $characters) in the text.
[address1 [, address2]] l [address1 [, address2]] l [len]
To test this command, we first replace the spaces in books.txt with tab.
$sed's / /\ t _ books.txt > junk.txt
Then execute the l command
$sed-n 'l'junk.txt 1)\ tStorm\ tof\ tSwords,\ tGeorge\ tR.\ tR.\ tMartin,\ t1216\ t$ 2)\ tThe\ tTwo\ tTowers,\ tJ.\ tTolkien,\ t352\ t$ 3)\ tThe\ tAlchemist,\ tPaulo\ tCoelho,\ t197\ t$ 4)\ tThe\ tFellowship\ tof\ tthe\ tRing,\ tJ.\ tR.\ tR.\ tTolkien,\ t432\ t$ 5)\ tThe\ tPilgrimage,\ tPaulo\ tCoelho,\ t288\ t$ 6)\ tA\ tGame\ tof \ tGeorge\ tR.\ tR.\ tMartin,\ t864 $
An interesting feature when using the l command is that we can use it to wrap the text to a specified width.
$sed-n'l 25' books.txt 1) Storm of Swords, Geor\ ge R. R. Martin, 1216 $2) The Two Towers, J. R.\ R. Tolkien, 352 $3) The Alchemist, Paulo\ Coelho, 197 $4) The Fellowship of the\ Ring, J. R. R. Tolkien,\ 432 $5) The Pilgrimage, Paulo\ Coelho, 288 $6) A Game of Thrones, Ge\ orge R. R. Martin, 864
In the above example, the l command is followed by the number 25, which tells SED to wrap according to 25 characters per line, and if you specify a number of 0, the newline will only be done if there is a newline character.
The l command is part of GNU-SED and may not be available in some other variants.
Exit command Q
In SED, you can exit the current execution flow using the Quit command
[address] q [address] q [value]
It should be noted that the Q command does not support an address range, only a single address match. By default, SED will follow the read, execute, and repeat workflow, but when it encounters the Q command, it exits the current execution flow.
$sed '3q' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197$ sed' / The Alchemist/ Q 'books.txt 1) A Storm of Swords, George R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197
The Q command also supports providing a value, which value will be returned as the return code of the program
$sed'/ The Alchemist/ Q 100' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197$ echo $? 100
File read command r
In SED, we can have SED read from an external file using the Read command and display it when the condition is met.
[address] r file
It is important to note that there must be only one space between the r command and the file name.
The following example opens the junk.txt file and inserts its contents after the third line of the books.txt file
$echo "This is junk text." > junk.txt $sed'3r junk.txt' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197 This is junk text. 4) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
The r command also supports address ranges. For example, 3,5 r junk.txt inserts the contents of junk.txt after the third, fourth, and fifth lines, respectively.
Execute external command e
If you've read the article learning AWK in 30 minutes, you probably already know that external commands can be executed in AWK, can we do the same in SED?
The answer is yes. In SED, we can use the e command to execute external commands.
[address1 [, address2]] e [command]
The following command executes the date command before the third line
$sed'3e date' books.txt 1) Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 Tuesday, November 29, 2016, 22:46:14 CST 3) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 2886) A Game of Thrones, George R. Martin, 864
Another example
$sed'3 mylxsw console Nov 5 e who' books.txt 1) Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 mylxsw console Nov 29 19:30 mylxsw ttys000 Nov 29 22:45 3) The Alchemist, Paulo Coelho, 197 mylxsw console Nov 29 19:30 mylxsw ttys000 Nov 29 22:45 4) The Fellowship of the Ring, J. R. Tolkien, 432 mylxsw console Nov 29 19:30 mylxsw ttys000 Nov 29 22:45 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones George R. R. Martin, 864
If you look closely at the syntax of the e command, you will find that its command parameter is actually optional. When no external command is provided, SED takes the contents of the schema space as the command to execute.
$echo-e "date\ ncal\ nuname" > commands.txt $cat commands.txt date cal uname $sed 'e'commands.txt November 29th, Tuesday, 22:50:30 CST November 2016, one, two, three, four, five, six, 12, three, five, seven, eight, nine, 10, 11, 12, 13, 14, 15, 16, 17, 19, 21, 23, 25, 26, 28, 29, 30 Darwin
Rule out the order!
Exclamation point command (!) Used to exclude commands, that is, to invalidate commands that would otherwise work.
$sed-n'/ Paulo/p' books.txt 3) The Alchemist, Paulo Coelho, 1975) The Pilgrimage, Paulo Coelho, 288$ sed-n'/ Paulo books.txt 1) Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3524) The Fellowship of the Ring, J. R. Tolkien, 4326) A Game of Thrones, George R. R. Martin, 864
As shown in the example above, the p command used to output only lines that match Paulo, add! After that, only lines that do not match the Paulo are output.
$sed-n'1g; h; $p 'books.txt 6) A Game of Thrones, George R. R. Martin, 864 5) The Pilgrimage, Paulo Coelho, 288 4) The Fellowship of the Ring, J. R. Tolkien, 4323) The Alchemist, Paulo Coelho, 1972) The Two Towers, J. R. Tolkien, 3521) Storm of Swords, George R. R. Martin, 1216
The above command implements output similar to the tac command, outputting the text in reverse order. It seems a little obscure, but it's very simple to break it down:
The sentence "1cm G" means that when processing each line, the contents of the retention space are appended to the pattern space (positive order-> reverse order).
H copy the contents of the pattern space to the reserved space in case the next line matches, append it to the next line
$p outputs the contents of the pattern space if it matches to a single line
The above steps are repeated until the end of the text just flips the contents of the file once.
Multiline command
You may have noticed a limitation when using the basic commands of the sed editor. All sed editor commands perform operations on a single row of data. When the sed editor reads the data stream, it divides the data into rows based on the position of the newline character. The sed editor processes one row of data at a time according to the defined script commands, and then moves to the next line to repeat the process.
Fortunately, the designers of the sed editor have taken this situation into account and designed the corresponding solution. The sed editor contains three special commands that can be used to work with multiline text.
N: add the next row in the data stream to create a multiline group to process
D: delete a row in a multiline group
P: print one line in a multiline group
N-load the next row
By default, SED operates on a single line, and in some cases we may need to use multiple lines for editing, and enable multi-line editing using the N command. Unlike n, N does not clear and output the contents of the mode space, but uses append mode.
[address1 [, address2]] N
The following example will display the title and author in books2.txt on the same line, separated by commas
$sed'n; s /\ nAccord books2.txt A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin
D-Delete one of multiple lines
The sed editor provides a multi-line delete command D, which deletes only the * lines in the schema space. This command removes all characters up to the newline character, including the newline character.
$echo'\ nThis is the header line.\ nThis is a data line.\ n\ nThis is the last line.' | sed'/ ^ $/ {N; / header/D} 'This is the header line. This is a data line. This is the last line.
P-one of the multiple lines of output
The P command is used to output * * lines in the pattern space of the multiline text created by the N command.
[address1 [, address2]] P
For example, the following command outputs only the title of the book
$sed-n'Ntera P'books2.txt A Storm of Swords The Two Towers The Alchemist The Fellowship of the Ring The Pilgrimage A Game of Thrones
Other commands
N-single line next
The lowercase n command tells the sed editor to move to the next line of text in the data stream and overwrites the line in the current mode space.
$cat data1.txt This is the header line. This is a data line. This is the last line. $sed'/ header/ {n; d} 'data1.txt This is the header line. This is a data line. This is the last line.
In the above command, the line containing header is matched first, then it is moved to the next line of the data flow, where there is a blank line, and then the d command is executed to delete the line break, so you can see the result: * blank lines have been deleted.
V-SED version check
The v command is used to check the version of SED. If the version is greater than the version in the parameter, it will be executed normally, otherwise it will fail.
[address1 [, address2]] v [version]
For example
$sed-- version sed (GNU sed) 4.2.2$ sed'v 4.2.3' books.txt sed:-e expression # 1, char 7: expected newer version of sed $sed'v 4.2.2 'books.txt 1) Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones George R. R. Martin, 864
Special character
Two special characters are provided in SED that can be used as commands: = and &.
= command
= the command is used to output the line number, and the syntax format is
[/ pattern/] = [address1 [, address2]] =
For example, output a line number for each line
$sed'= 'books2.txt 1 A Storm of Swords 2 George R. R. Martin.
Output line numbers for only 1-4 lines
$sed '1,4' books2.txt 1 A Storm of Swords 2 George R. R. Martin 3 The Two Towers 4 J. R. Tolkien The Alchemist Paulo Coelho The Fellowship of the Ring J. R. Tolkien The Pilgrimage Paulo Coelho A Game of Thrones George R. R. Martin
The line output line number that matches the Paulo
$sed'/ Paulo/ = 'books2.txt A Storm of Swords George R. R. Martin The Two Towers J. R.. Tolkien The Alchemist 6 Paulo Coelho The Fellowship of the Ring J. R. R.. Tolkien The Pilgrimage 10 Paulo Coelho A Game of Thrones George R. R. Martin
* one line output line number, this command is more interesting, how many lines can be used to output the file?
$sed-n'$= 'books2.txt 12
& Command
Special characters & used to store the contents of matching patterns, usually with the replacement command s.
$sed's / [: digit:] / Book number & / 'books.txt Book number 1) Storm of Swords, George R. R. Martin, 1216 Book number 2) The Two Towers, J. R. Tolkien, 352 Book number 3) The Alchemist, Paulo Coelho, 197 Book number 4) The Fellowship of the Ring, J. R. Tolkien, 432 Book number 5) The Pilgrimage, Paulo Coelho, 288 Book number 6) A Game of Thrones, George R. Martin, 864
The above command is used to match each line of * digits and precede it with Book number. The following command matches a number of * * and changes it to Pages =. Where [[: digit:]] * * $may be confusing, this part is actually: match 0 or more digits + 0 or more spaces + line endings.
Sed's / [[: digit:]] * * $/ Pages = & / 'books.txt 1) Storm of Swords, George R. R. Martin, Pages = 1216 2) The Two Towers, J. R. R. Tolkien, Pages = 3523) The Alchemist, Paulo Coelho, Pages = 1974) The Fellowship of the Ring, J. R. R. Tolkien, Pages = 4325) The Pilgrimage, Paulo Coelho, Pages = 2886) A Game of Thrones, George R. R. Martin, Pages = 864
String
Replace command s
Text replacement commands are very common and have the following format
[address1 [, address2]] s/pattern/replacement/ [flags]
In the books.txt file we used earlier, we used commas to separate each column. In the following example, we will use the replace command to replace it with the pipe character "|":
$sed's Martin / / 'books.txt 1) Storm of Swords | George R. R. Martin, 1216 2) The Two Towers | J. R. R. Tolkien, 3523) The Alchemist | Paulo Coelho, 1974) The Fellowship of the Ring | J. R. R. Tolkien, 4325) The Pilgrimage | Paulo Coelho, 2886) A Game of Thrones | George R. Martin, 864
Do you think there's something wrong? I believe you have found that the second comma of each line has not been replaced, only * * has been replaced, indeed, in SED, only * * matching positions will be replaced by default when using the replace command. Use the g option to tell SED to replace everything:
$sed's books.txt Martin / | George R. Martin | 1216 2) The Two Towers | J. R. R. Tolkien | 3523) The Alchemist | Paulo Coelho | 1974) The Fellowship of the Ring | J. R. R. Tolkien | 4325) The Pilgrimage | Paulo Coelho | 288 6) A Game of Thrones | George R. R. Martin | 864
If you replace the lines of the matching pattern (or address range), you only need to add the address before the s command. For example, replace only the lines that match The Pilgrimage: sed'/ The Pilgrimage/ _ books.txt / | / g 'line
There are some other options, which are briefly described here and will not be explained.
The number n: replace only the nth match, such as sed's _
P: output only changed lines, such as sed-n 's/Paulo Coelho/PAULO COELHO/p' books.txt
W: stores changed lines to a file, such as sed-n 's/Paulo Coelho/PAULO COELHO/w junk.txt' books.txt
I: ignore case when matching, such as sed-n 's/pAuLo CoElHo/PAULO COELHO/pi' books.txt
When performing a replace operation, what if the content you want to replace contains /? It's simple to add an escape operator.
$echo "/ bin/sed" | sed's /\ / bin\ / sed/\ / home\ / mylxsw\ / src\ / sed\ / sed-4.2.2\ / sed/' / home/mylxsw/src/sed/sed-4.2.2/sed
In the above command, we escaped / with `, but the expression already looks very ugly, and you can also use |, @, ^,!` as the command delimiter in SED, so the following commands are equivalent to the above
Echo "/ bin/sed" | sed's | / bin/sed | / mylxsw/mylxsw/src/sed/sed-4.2.2/sed | 'echo "/ bin/sed" | sed's echo / bin/sed "| sed's echo / bin/sed" | sed's ^ / bin/ sed^ / home/mylxsw/src/sed/sed-4.2.2/ sed^' echo "/ bin/sed" | Sedhammer homebank mylxswqr srcUnip sedUniverse sedmer 4.2.2 Universe sedbath'
Match substring
Now that we've learned the use of the replace command, let's see how to get a substring in the matching text.
In SED, matching content is grouped using (and) and referenced in an N way. See the following example
$echo "Three One Two" | sed's |\ (\ w\ +)\ (\ w\ +\)\ (\ w\ +\) |\ 2\ 3\ 1 | 'One Two Three
We output three words of Three,One,Two, and in the replacement rules of SED, we use spaces to separate three small regular expressions (w+) to match each word, followed by a reference to their values using 1, 1, and 2, respectively.
Management mode
We have already explained the use of pattern space and retention space, and we will continue to explore their usage in this section.
This section has not been updated yet. Please pay attention to the programmer growth plan project. I will update the content in this warehouse in Github.
Regular expression
This part is the standard regular expression of some special characters to metacharacters, more familiar with please skip.
Standard regular expression
^
The start of the match line.
$sed-n'/ ^ The/ p 'books2.txt The Two Towers, J. R. R. Tolkien The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien The Pilgrimage, Paulo Coelho
$
Match the end of the line
$sed-n'/ Coelho$/ p 'books2.txt The Alchemist, Paulo Coelho The Pilgrimage, Paulo Coelho
.
Match a single character (except at the end of a line)
$echo-e "cat\ nbat\ nrat\ nmat\ nbatting\ nrats\ nmats" | sed-n'/ ^.. tasking and p'cat bat rat mat
[]
Matching character set
$echo-e "Call\ nTall\ nBall" | sed-n'/ [CT] all/ p 'Call Tall
[^]
Exclude character set
$echo-e "Call\ nTall\ nBall" | sed-n'/ [^ CT] all/ p 'Ball
[-]
Character range.
$echo-e "Call\ nTall\ nBall" | sed-n'/ [Cmurz] all/ p 'Call Tall
?,\ +, *
Correspond to 0 to 1, one to many, and 0 to many matches, respectively.
{n}, {n,}, {m, n}
Exactly match N times, match at least N times, match Mmurn times
| |
Or operation.
$echo-e "str1\ nstr2\ nstr3\ nstr4" | sed-n'/ str\ (1\ | 3\) / p 'str1 str3
POSIX compatible regularities
It mainly includes [: alnum:], [: alpha:], [: blank:], [: digit:], [: lower:], [: upper:], [: punct:], [: space:].
Metacharacter
S
Match a single blank content
$echo-e "Line\ T1\ nLine2" | sed-n'/ Line\ s / p 'Line 1
S
Matches a single non-blank content.
W, W
Single word, non-word.
Common code snippet
Cat command
Simulating the cat command is relatively simple, and there are two ways to simulate it
$sed''books.txt 1) Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 1974) The Fellowship of the Ring, J. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. Martin, 864$ sed-n'p 'books.txt 1) Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. Tolkien, 3523) The Alchemist Paulo Coelho, 1974) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
Remove blank lines
$echo-e "Line # 1\ n\ n\ nLine # 2" | sed'/ ^ $/ d'Line # 1 Line # 2
Delete consecutive blank lines
$echo-e "Line # 1\ n\ n\ nLine # 2" | sed'/. /, / ^ $/! D'Line # 1 Line # 2
Delete the blank line at the beginning
$echo-e "\ nLine # 1\ n\ nLine # 2" | sed'/. /, $! d'Line # 1 Line # 2
Delete the blank line at the end
$echo-e "\ nLine # 1\ nLine # 2\ n\ n" | sed': start / ^\ nshipping / {$d; N; b start} 'Line # 1 Line # 2
Filter all html tags
$cat html.txt This is the page title
This is the first line in the Web page. This should provide some useful information to use in our sed script. $sed's /] * > / / g; / ^ $/ d'html.txt This is the page title This is the first line in the Web page. This should provide some useful information to use in our sed script.
Remove comments from the C++ program
There is a cpp file like this
$cat hello.cpp # include using namespace std; int main (void) {/ / Displays message on stdout. Cout > > "Hello, World!!" > > endl; return 0; / / Return success. }
Execute the following command to remove comments
$sed's | / /. * | | g 'hello.cpp # include using namespace std; int main (void) {cout > > "Hello, World!!" > endl; return 0;}
Add comments to some Lin
$sed '3hostname 5s / ^ / # /' hello.sh #! / bin/bash # pwd # hostname # uname-a who who-r lsb_release-a
Implement the Wc-l command
The wc-l command is used to count the number of lines in a file, which can also be simulated using SED
$wc-l hello.cpp 9 hello.cpp $sed-n'$= 'hello.cpp 9
Simulate and implement head command
The head command is used to output the first 10 lines of the file.
$head books2.txt A Storm of Swords George R. R. Martin The Two Towers J. R. R. Tolkien The Alchemist Paulo Coelho The Fellowship of the Ring J. R. R. Tolkien The Pilgrimage Paulo Coelho
Its implementation can be simulated using sed '10q' in SED
$sed '10q' books.txt A Storm of Swords George R. R. Martin The Two Towers J. R. Tolkien The Alchemist Paulo Coelho The Fellowship of the Ring J. R. Tolkien The Pilgrimage Paulo Coelho
Simulate tail-1 command
The * * line of the tail-1 output file.
$cat test.txt Line # 1 Line # 2$ tail-1 test.txt Line # 2$ sed $sed-n'$p 'test.txt Line # 2
Simulate Dos2unix command
In the DOS environment, newline characters are represented by both CR/LF characters. The following command simulates the dos2unix command to convert these newline characters to UNIX newline characters.
In the GNU/Linux environment, CR/LF is usually represented by "^ M" (not a simple combination of two symbols, please use the shortcut key Ctrl+v,Ctrl+m input).
$echo-e "Line # 1\ r\ nLine # 2\ r" > test.txt $file test.txt test.txt: ASCII text, with CRLF line terminators $sed's / ^ M $/ / 'test.txt > new.txt $file new.txt new.txt: ASCII text $cat-vte new.txt Line # 1$ Line # 2 $
Simulate Unix2dos command
$file new.txt new.txt: ASCII text $sed's new.txt /\ r Line 'new.txt > new2.txt $file new2.txt new2.txt: ASCII text, with CRLF line terminators $cat-vte new2.txt Line # 1 ^ M $Line # 2 ^ M $
Simulate the cat-E command
The cat-E command outputs a $symbol at the end of each line.
$echo-e "Line # 1\ nLine # 2" | cat-E Line # 1$ Line # 2 $$echo-e "Line # 1\ nLine # 2" | sed's | $| & $| 'Line # 1$ Line # 2 $
Note that cat-E is not supported under Mac, so you can directly use sed instead of
Simulate the cat-ET command
The cat-ET command not only adds $to the end of each line, but also displays the TAB in each line as ^ I.
$echo-e "Line # 1\ tLine # 2" | cat-ET Line # 1 ^ ILINE # 2$ echo-e "Line # 1\ tLine # 2" | sed-n 'l' | sed'y /\\ t / I / 'Line # 1 ^ ILINE # 2 $
Simulate nl command
The command nl adds a line number to each line of input. Remember the = operator introduced earlier, which we can use to achieve similar functionality to the nl command in SED.
$echo-e "Line # 1\ nLine # 2" | nl 1 Line # 1 2 Line # 2$ echo-e "Line # 1\ nLine # 2" | sed = | sed'Nmitts /\ n /\ t Line'1 Line # 1 2 Line # 2
The above SED command is used twice, and the = operator is used * * times to output a line number for each line. Note that this line number is exclusive to one line, so you connect the second SED command with a pipe character, reading two lines at a time, replacing the newline character with Tab, which simulates the effect of the nl command.
Simulate cp command
$sed-n'd dup.txt' data.txt $diff data.txt dup.txt $echo $? 0
Simulate expand command
The expand command converts the TAB in the input to a space, which can also be simulated in SED
$echo-e "One\ tTwo\ tThree" > test.txt $expand test.txt > expand.txt $sed's /\ t / / g 'test.txt > new.txt $diff new.txt expand.txt $echo $? 0
Simulate tee command
The tee command outputs the data to standard output while writing to the file.
$echo-e "Line # 1\ nLine # 2" | tee test.txt Line # 1 Line # 2
In SED, implementing this command is very simple
$sed-n'p; w new.txt' test.txt One Two Three
Simulate cat-s command
The cat-s command merges multiple lines of spaces in the input file into one line.
$echo-e "Line # 1\ n\ nLine # 2\ n\ n\ nLine # 3" | cat-s Line # 1 Line # 2 Line # 3
Implemented in SED
$echo-e "Line # 1\ n\ nLine # 2\ n\ n\ nLine # 3" | sed '1s/ ^ $/ / pexipachap. Line /! D'Line # 1 Line # 2 Line # 3
It is important to note that the command /. /, / ^ $/! d means to match the interval /. / to / ^ $, the beginning of the interval will match the line containing at least one character, the end will match a blank line, and the lines in this interval will not be deleted.
Simulate grep command
$echo-e "Line # 1\ nLine # 2\ nLine # 3" | grep 'Line # 1' Line # 1$ echo-e "Line # 1\ nLine # 2\ nLine # 3" | sed-n'/ Line # 1 and p'Line # 1
Simulate the grep-v command
$echo-e "Line # 1\ nLine # 2\ nLine # 3" | grep-v 'Line # 1' Line # 2 Line # 3$ echo-e "Line # 1\ nLine # 2\ nLine # 3" | sed-n'/ Line # 1 and sed p'Line # 2 Line # 3
Simulate tr command
The tr command is used for character conversion
$echo "ABC" | tr "ABC"abc" abc $echo "ABC" | after reading the above contents, have you mastered how to learn SED? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.