In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use the sed command in linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Description:
Sed is an abbreviation for stream editor (stream editor). It perfectly matches regular expressions. Sed and awk are the two most important commands for file editing. In particular, it involves a lot of regular expressions, the author does not dare to be a little timid, try to write.
Example:
1. Replace the string in the file
$sed-I's _
# if you don't end with g, replace the first of each line
# if it's just printing, remove-I
two。 Ignore the match at the first N and start the replacement from Numb1.
$sed-I's text _ file _ _ replace _ file
# add the number N before g
3. Remove white space character
$sed'/ ^ $/ d'file
4. Mark matched content
$echo this is an example | sed's:\ w\ +: / [&]: G'
[this] [is] [an] [example]
# colons are delimiters, which can be arbitrary as long as appropriate
5. Substring matching
$echo this is digit 7 in a number | sed 's:digit\ (0-9\):\ 1VOR'
# output: this is 7 in a number
# anyone who has studied regular expressions understands the concept of group. The content in () is the first group, so only 7 is printed.
6. Quote
$text=hello
$echo hello world | sed "s/$text/HELLO"
# output HELLO world
# some people may notice the use of double quotes because single quotes treat $text as a string rather than an expression.
7. Delete
$sed '2d' file # Delete the second line
$sed'2 file # Delete 2-Last Line
$sed'$d' file # Delete the last line
Interpretation-help
Usage: sed [option]. {script (if there is no other script)} [input file].
-n,-- quiet,-- silent
Cancel automatic printing mode space
-e script,-- expression= script
Add "script" to the running list of the program
-f script file,-- file= script file
Add "script file" to the running list of the program
-- follow-symlinks
Follow the soft link when modifying the file directly
-I [extension],-- in-place [= extension]
Modify the file directly (back up the file if you specify an extension)
-l N,-- line-length=N
Specify the expected length of the line wrap for the "l" command
-- posix
Turn off all GNU extensions
-r,-- regexp-extended
Use extended regular expressions in scripts
-s-- separate
Treat the input file as a separate file rather than a long continuous input
-u,-- unbuffered
Read the least amount of data from the input file and refresh the output more frequently
-- help print help and exit
-- version outputs version information and exits
If there is no-e,-- expression,-f, or-- file option, then the first non-option parameter is considered
Sed script. Other non-option parameters are treated as input files, and if there is no input file, the program will derive from the standard
Enter read data.
Interpretation:
It is true that there is not enough information about this help, but sed is really too complex, so try to explain the command in detail several times.
I'll borrow another help document.
There are two forms of invoking the sed command:
Sed [options] 'command' file (s)
Sed [options]-f scriptfile file (s)
Options
A\ example: sed "a\ mm" file
Add a line of text after the current line.
B lable
Branch to the marked place in the script, or to the end of the script if the branch does not exist.
C\
Change the text of this line with the new text.
D
Delete rows from the Pattern space location.
D
Delete the first line of the template.
I\
Inserts text above the current line.
H
Copy the contents of the mold plate to the buffer in memory.
H
Append the contents of the template block to the buffer in memory
G
Gets the contents of the memory buffer and replaces the text in the current module plate.
G
Gets the contents of the memory buffer and appends it to the text of the current module block.
L
A list cannot print a list of characters.
N
Read the next input line and process the new line with the next command instead of the first command.
N
Append the next input line to the template block and embed a new line between the two to change the current line number.
P
Print the lines of the template plate.
P (uppercase)
Print the first line of the template plate.
Q
Exit Sed.
R file
Read lines from file.
T label
The if branch, starting on the last line, will result in a branch to the labeled command, or to the end of the script, once the condition is met or the TMaget command is met.
T label
The error branch, starting at the last line, will result in a branch to the labeled command or to the end of the script in the event of an error or the TMaget command.
W file
Write and append the template to the end of the file.
W file
Write and append the first line of the template block to the end of file.
!
Indicates that subsequent commands have an effect on all rows that have not been selected.
S/re/string
Replace the regular expression re with string.
=
Print the current line number.
#
Extends the comment before the next newline character.
The following is the replacement tag
G means full in-line replacement.
P represents a print line.
W means to write a line to a file.
X represents the text in the interchange mode plate and the text in the buffer.
Y means to translate one character into another (but not for regular expressions)
The difference between adding a diagonal bar and not adding it
For example: a\ means to add content after a\, while d adds an expression before d, and 1d means to delete 1 line.
Sed supplement
This is a more comprehensive supplement to sed commands and options, mainly examples collected from the Internet.
1 p command
The command p is used to display the contents of the mode space. By default, sed prints input lines on the screen, and the option-n is used to cancel the default print operation. When the option-n and the command p appear at the same time, sed can print the selection.
The code is as follows:
Sed'/ my/p' datafile
# by default, sed prints all input lines on standard output. If a line matches the pattern, the my,p command prints the line again.
Sed-n'/ my/p' datafile
The # option-n cancels the default printing of sed, and the p command prints the lines that match the pattern my.
2.d command
The command d is used to delete the input line. Sed copies the input line from the file to the pattern space, then executes the sed command on the line, and finally displays the contents of the pattern space on the screen. If the command d is issued, the input line in the current mode space is deleted and not displayed.
Sed'$d' datafile
# Delete the last line and the rest are displayed
Sed'/ my/d' datafile
# Delete the line containing my, and the rest are displayed
3.s command
Sed's / ^ my / You/g' datafile
The g at the end of the # command indicates a global replacement within the line, that is, if there is more than one My on a line, all My are replaced with You.
Sed-n'1 Magi 20s Universe GPP 'datafile
# cancel the default output, deal with 1 to 20 lines that match lines ending in My, replace all My in the line with You, and print it to the screen.
Sed's roomMysterYourly datafile
The character immediately following the s command is the delimiter between the lookup string and the replacement string. The delimiter defaults to a forward slash, but can be changed. No matter what character (except newline or backslash), as long as you follow the s command, it becomes the new string delimiter.
4 e option
-e is an editing command that is used when sed performs multiple editing tasks. Before the next line starts editing, all editing actions are applied to the lines in the pattern buffer.
Sed-e '1mlmt 10dlmlmlle e's Universe MyUnique YourUnique g' datafile
The # option-e is for multiple edits. The first editor deletes lines 1-3. The second editor replaces all My that appears with Your. Because these two edits are made line by line (that is, both commands are executed on the current line of the mode space), the order of the editing commands affects the result.
5 r command
The r command is a read command. Sed uses this command to add the contents of a text file to a specific location in the current file.
Sed'/ My/r introduce.txt' datafile
# if the pattern My is matched on a line of the file datafile, the contents of the file introduce.txt are read after that line. If there is more than one line of My, the contents of the introduce.txt file are read in after each line of My appears.
6w command
Sed-n'/ hrwang/w me.txt' datafile
7 a\ command
The a\ command is an append command that adds new text to the file after the current line (that is, the line read into the pattern buffer). The appended line of text is on another line below the sed command. If you want to append more than one line, each line must end with a backslash, except the last line. The last line ends with quotation marks and a file name.
Sed'/ ^ hrwang/a\
> hrwang and mjfan are husband\
> and wife' datafile
# if a line that matches a line that begins with hrwang is found in the datafile file, append hrwang and mjfan are husband and wife under the line
8 I\ command
The I\ command inserts new text before the current line.
9 c\ command
Sed uses this command to modify existing text to new text.
10 n command
Sed uses this command to get the next line of the input file and read it into the pattern buffer, and any sed command will be applied to the next line immediately following the matching line
Sed'/ hrwang/ {nintersUniverse;} 'datafile
Note: if you need to use multiple commands, or if you need to nest addresses within an address range, you must enclose the commands in curly braces, write only one command per line, or separate multiple commands on the same line with a semicolon.
11y command
This command is similar to the tr command in UNIX/Linux, where characters are converted from left to right on an one-to-one basis. For example, y/abc/ABC/ will convert all lowercase a to A, lowercase b to B, and lowercase c to C.
Hrwang12 / hrWANG^ $/ 'datafile
# convert all lowercase hrwang from 1 to 20 lines to uppercase, 1 to ^, and 2 to $.
The # regular expression metacharacter does not work on the y command. Like the delimiter of the s command, slashes can be replaced with other characters.
12Q command
The Q command will cause the sed program to exit without any other processing.
01.sed'/ hrwang/ {datafile hrwang; hrwang
13 h command and g command
# cat datafile
My name is hrwang.
Your name is mjfan.
Hrwang is mjfan's husband.
Mjfan is hrwang's wife.
Sed-e'/ hrwang/h'-e'$G' datafile
Sed-e'/ hrwang/H'-e'$G' datafile
# through the above two commands, you will find that h will clear the contents of the original temporary buffer and save only the contents of the mode space saved during the last execution of h. The H command appends each line that matches hrwnag to a temporary buffer.
Sed-e'/ hrwang/H'-e'$g 'datafile
Sed-e'/ hrwang/H'-e'$G' datafile
# through the above two commands, you will find that g replaces the contents of the current line in the pattern space with the contents of the staging buffer, which is the last line here. The G command appends the contents of the temporary buffer to the current line of the mode space. This is appended to the end.
This is the end of this article on "how to use sed commands in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.