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)05/31 Report--
This article is about how to use sed in shell. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. First of all, let's find out what sed is.
Sed is called a stream editor, and it is commonly used as a filter in shell scripts and Makefile, that is, the output of the previous program is introduced into the input of sed and converted into another format through a series of editing commands. Let's take a look at how it works through a schematic:
The format of the editing command is sed / pattern/action
The contents of the file have not changed after sed processing, except that redirection is used to store the output. Sed is mainly used to automatically edit one or more files; simplify repeated operations on files; sed basically matches according to the Basic specification by default! In other words, special characters such as () {} | need to be escaped, otherwise they will not be recognized, or extended mode can be used.
two。 The following focuses on the parameters of sed and the operation method of action
(1)-n parameter, action of p command
(2) action of d command
(3) / pattern/s/pattern1/pattern2/: looks for a line that matches pattern, and replaces the line * strings matching pattern1 with pattern2
/ pattern/s/pattern1/pattern2/g: find the line that matches pattern and replace all strings in the line that match pattern1 with pattern2
I don't know if you will think of the bottom-line pattern search in the vim editor. They are similar.
The following is a summary of the selection and execution of parameters in sed:
[plain] view plain copy
Parameter selection:
-n: normally, the sed command will output all the data to the screen. If you add the-n option, only the lines processed by the sed command will be output to the screen.
-e: allows multiple sed command edits to be applied to input data.
-I: writes the modification result directly to the file that reads the data, rather than outputting it from the screen. (1. Modified the file; 2. Can be viewed by cat)
-f: specifies the file name of the sed script.
Action:
A: append, adding one or more lines after the current line.
C: line substitution, replacing the original data row with the string after c.
I: insert, insert one or more rows before the current row.
P: print and output the specified line.
S: string substitution, replacing one string with another. The format is "line range s / old string / new string / g" (if no g is added, only * matching strings per line will be replaced).
If you are interested in what you have not practiced above, you can try it!
3. Let's introduce the addressing in sed again.
The address is used to determine which lines in the file to edit. The address can be in the form of a number, a regular expression, or a combination of both. If no address is specified, sed processes all lines of the input file. Here are some examples:
Sed'/ start/, / end/d' file # Delete rows that contain lines between 'start'' and 'end'' lines
Sed'/ start/, 10d' file # deletes the line containing 'start' through line 10
4. Pattern space and hold space
(1) hold space: it is used to store data, which is equivalent to a warehouse, which cannot process the data.
(2) pattern space: data are processed exclusively by behavior units.
In general, space retention will not be used if some options are not shown.
[plain] view plain copy
Command:
G: copying the contents of the hold space to the pattern space will overwrite the original value of the pattern space.
G: appends the contents of the reserved space to the schema space.
H: copying the value of the pattern space to the hold space will overwrite the original value of the hold space.
H: appends the value of the pattern space to the hold space.
D: delete all rows in the pattern space and read the next line to the pattern space.
D: delete the * line in the pattern space and do not read the next line to the pattern space.
N: output the line of the pattern space, read the next line to replace the line of the current pattern space, and then execute the next processing command instead of the * command.
N: read the next line and append it to the pattern space line, where there are two lines in the pattern space.
X: swap the contents of mode space and hold space.
Example 1: add a blank line after each line
Example 2: using sed to simulate the process of tac printing
Example 3. Append matching lines to the end of the file
Example 4: change a column into a row
Example 5: find the sum of 1-100
Example 6: print odd and even lines
5. Use label
[plain] view plain copy
: a means label a
Ba means to jump to the a tag
$represents * line
! Indicates that no follow-up actions are taken.
So, $! ba means that the * * line does not need to jump to the a tag to end the operation.
Here is an example:
* one additional point:
[plain] view plain copy
Like grep, sed also supports special metacharacters for pattern search and replacement. The difference is that the regular expression used by sed is a pattern enclosed between the slash line "/".
If you want to change the regular expression delimiter "/" to another character, such as o, simply precede the character with a backslash, follow the regular expression, and then follow the character. For example: sed-n'\ o ^ 56op 'datafile
^: the line beginning locator / ^ my/ matches all lines that begin with my
$: the end of line locator / my$/ matches all lines ending in my
.: matches a single character other than a newline character / m..y/ matches a line containing the letter m, followed by two arbitrary characters, followed by the letter y
*: match zero or more leading characters / test*/ matches a line containing the string tes followed by zero or more t letters
[]: matches any character / t [E] st/ within the specified character group matches the line containing test or tEst
[^]: matches any character that is not in the specified character group / t [^ eE] st/ matches string starts with t, but the character before st is not the line of e or E.
&: save the lookup string to reference the s/test/*&*/g symbol in the replacement string & which represents the lookup string. Test will be replaced with * test*
\ / match lines that contain words ending in my
X\ m\}: M consecutive x such as: / 9\ {5\} / match lines containing 5 consecutive 9s
X\ m,\}: at least m x such as: / 9\ {5,\} / matches lines containing at least 5 consecutive 9s
X\ {mdirection n\}: at least m, but no more than n x such as: / 9\ {5 ~ 7\} / matching contains 5 to 7 consecutive 9s. There is also the problem of unit matching-replacement:
Thank you for reading! This is the end of the article on "how to use sed in shell". 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, you can 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.