One day a shell command Linux text content Operation series-sed command detailed _linux Shell

Source: Internet
Author: User
Tags lowercase
Description:

sed is an abbreviation for stream editor. It can perfectly match regular expressions. Sed and awk are the two most important commands for file editing. In particular, it involves a lot of regular expression problems. I dare not be a bit guilty. I try to write.

Examples:

1. Replace the string in the file

$ sed -i 's / text / replace / g' file

#If not ending with g, replace the first of each line

#If just printing, remove -i

2. Ignore the first N matches and start replacing from N + 1

$ sed -i 's / text / replace / 2g' file

#Add the number N in front of g

3. Remove white space

$ sed '/ ^ $ / d' file

4. Mark matched content

$ echo this is an example | sed 's: \ w \ +: / [&]: g'

[this] [is] [an] [example]

#Colon is a delimiter, as long as it is suitable, the delimiter can be any

5. Substring matching

$ echo this is digit 7 in a number | sed 's: digit \ (0-9 \): \ 1:'

#Output: this is 7 in a number

#People who have learned regular expressions all understand the concept of group, the content in () is the first group, so only 7 is printed

6. Reference

$ text = hello

$ echo hello world | sed "s / $ text / HELLO"

#Output HELLO world

#Maybe some people pay attention to using double quotes because single quotes will treat $ text as a string rather than an expression.

7. Delete

$ sed '2d' file #Delete the second line

$ sed '2, $ d' file #Delete 2-last line

$ sed '$ d' file #Delete the last line

Interpretation—help

Usage: sed [options] ... {script (if there is no other script)} [input file] ...

  -n, --quiet, --silent
                  Cancel automatic print mode space
  -e script, --expression = script
                 Add "Script" to the run list of the program
  -f script file, --file = script file
                 Add "script file" to the run list of the program
  --follow-symlinks
                  Follow soft links when directly modifying files
  -i [extension], --in-place [= extension]
                  Modify the file directly (back up the file if you specify the extension)
   -l N, --line-length = N
                  Specify the expected length of line breaks for the "l" command
  --posix
                  Close all GNU extensions
  -r, --regexp-extended
                  Use extended regular expressions in scripts
  -s, --separate
                  Treat input files as separate files rather than one long continuous input
  -u, --unbuffered
                  Read the least data from the input file and refresh the output more frequently
      --help print help and exit
      --version output version information and exit

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 regarded as input files, if there is no input file, then the program will be from the standard
 Enter the read data.

Interpretation:

For this help information, the amount of information is indeed very insufficient, but sed is indeed too complicated, so try to explain this command in several times.

I will borrow another piece of help information

There are two forms of calling the sed command:
sed [options] 'command' file (s)
sed [options] -f scriptfile file (s)

options
a \ For example: sed "a \ mm" file
Add a line of text after the current line.
b lable
Branch to the marked place in the script. If the branch does not exist, branch to the end of the script.
c \
Change the text of this line with the new text.
d
Delete the line from the pattern block position.
D
Delete the first line of the template block.
i \
Insert text above the current line.
h
Copy the contents of the template block to the buffer in memory.
H
Append the contents of the template block to the buffer in memory
g
Get the contents of the memory buffer and replace the text in the current template block.
G
Get the contents of the memory buffer and append it to the current template block text.
l
The 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 back of the template block and embed a new line between the two to change the current line number.
p
Print the lines of the template block.
P (upper case)
Print the first line of the template block.
q
Exit Sed.
r file
Read lines from file.
t label
The if branch, starting from the last line, once the condition is satisfied or the T, t command will cause a branch to the labeled command or to the end of the script.
T label
The error branch starts from the last line. Once an error or T, t command occurs, it will cause a branch to the labeled command or to the end of the script.
w file
Write and append the template block to the end of the file.
W file
Write and append the first line of the template block to the end of the file.
!
Indicates that the following commands will affect all lines that are not selected.
s / re / string
Replace regular expression re with string.
=
Print the current line number.
##
Expand the comment before the next newline.


 The following are replacement tags
g means full replacement in the line.
p means print line.
w means write the line to a file.
x indicates that the text in the template block and the text in the buffer are interchanged.
y means to translate a character to another character (but not for regular expressions)

The difference between adding a diagonal bar and not adding
 For example: a \ means to add content after a \, and d to add expression before d, 1d means to delete 1 line.

sed supplement

This is a relatively comprehensive addition to the sed commands and options, mainly examples collected from the Internet.

1 p command

The command p is used to display the contents of the pattern space. By default, sed prints the input line on the screen, and the option -n is used to cancel the default printing operation. When the option -n and the command p appear at the same time, sed can print the selected content.
Copy the code 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 my, the p command will print the line again.

sed -n '/ my / p' datafile
 # Option-n cancel the default printing of sed, the p command prints the line matching the pattern my.
2.d command
The command d is used to delete the input line. sed first 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 will be deleted and not displayed.

 sed '$ d' datafile
#Delete the last line, the rest are displayed
sed '/ my / d' datafile
 #Delete the line containing my, the rest are displayed

3.s command

 sed 's / ^ My / You / g' datafile
 #The g at the end of the command means to perform global replacement within the line, that is to say, if there are multiple My in a line, all My are replaced by You.
sed -n '1,20s / My $ / You / gp' datafile
 #Cancel the default output, process the lines from 1 to 20 that match the end of My, replace all My in the line with You, and print to the screen.
 

sed 's # My # Your # g' datafile
 # The character immediately following the s command is the separator between the search string and the replacement string. The separator is a forward slash by default, but it can be changed. No matter what character (except newline and backslash), as long as it follows the s command, it becomes a new string separator.

4 e option

-e is an editing command, used when sed performs multiple editing tasks. Before the next line starts editing, all editing actions will be applied to the line in the pattern buffer.

 sed -e '1,10d'-e' s / My / Your / g 'datafile
  # Option-e is used for multiple editing. The first edit deletes lines 1-3. The second edit replaces all My that appear with Your. Because these two edits are performed line by line (that is, both commands are executed on the current line in the pattern space), the order of the editing commands will affect 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 a line in the file datafile matches the pattern My, read the content of the file intraduce.txt after the line. If there are more than one line of My, the contents of the intraduce.txt file are read after each line of My.

 6 w command

 sed -n '/ hrwang / w me.txt' datafile
 
7 a \ command
The a \ command is an append command, and appending will add new text to the end of the current line in the file (that is, the line read into the pattern buffer). The appended line of text is on a new line below the sed command. If the content to be appended exceeds one line, each line must end with a backslash, except for the last line. The last line will end with quotes and file name.

sed '/ ^ hrwang / a \
 > hrwang and mjfan are husband \
 > and wife 'datafile
 #If a line starting with hrwang is found in the datafile file, append hrwang and mjfan are husband and wife
 
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, any sed command will be applied to the next line immediately after the matching line

 sed '/ hrwang / {n; s / My / Your /;}' datafile
= Note: If you need to use multiple commands, or need to nest addresses within a certain address range, you must enclose the commands in curly braces, write only one command per line, or use a semicolon to separate multiple commands in the same line Order.

11 y command

This command is similar to the tr command in UNIX / Linux. Characters are converted from left to right in a one-to-one manner. For example, y / abc / ABC / will convert all lowercase a to A, lowercase b to B, and lowercase c to C.

 sed '1,20y / hrwang12 / HRWANG ^ $ /' datafile
 #Convert 1 to 20 lines, all lowercase hrwang to uppercase, 1 to ^, and 2 to $.
#Regular expression metacharacters have no effect on the y command. As with the s command separator, slashes can be replaced with other characters.
 
 12 q command
The q command will cause the sed program to exit and no other processing will be done
01.sed '/ hrwang / {s / hrwang / HRWANG /; q;}' datafile

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 only save the contents of the pattern space that was saved when h was last executed. The H command appends and saves each line matching hrwnag in the 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 temporary buffer with the contents of the current line in the pattern space, and here replaces the last line. The G command appends the contents of the temporary buffer to the current line of the pattern space. This is appended to the end.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.