This is the 11th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
sed
In the UNIX or Linux system, the use of pipeline operators |, multiple commands can be made together as “pipeline”. The information (data) that flows through a pipeline is called a stream.
(Note: This article was written many, many years ago, I don’t know what the reference is, “Pipeline”, “Pipeline”, “Pipeline”, “Pipeline”)
To editor modify information in a pipe, you use a stream editor — hence the name of the command sed.
Sed edits the file and outputs the result to 1 (stdout), leaving the original file unchanged.
Sed [options] 'quoted command expression' [input file]Copy the code
-e <expression>
: e is expression, which processes the input text file in the script specified in the option, followed by an expression-f <file>
: Processes the input text file as a script file specified in the option-i
: Directly replace in the file, not in the terminal output-i
Often used to back up the original files-l
Together with
If you use multiple command expressions in sed, use a semicolon (;) in the middle of the command expression. Separated.
Common command expressions enclosed in quotation marks include text replacement and deletion
Textual substitution
To extract a string within the range of specified rows of data in a file and replace it with a new schema:
sed -e 's/ old mode/new mode/flag ' file
Copy the code
s
Substitute means substitutemark
Commonly used are:g
(globally), meaning to replace all patterns that appear in each line. (No G only change the first of each row)n
Tell sed to replace only beforen
The pattern that appears in the row.
e.g.
grep CLERK emp.fmt | sed -e 's/ /; /g; s/CLERK/ASSISTANT MANAGER/g'Copy the code
- Grep finds all lines with CLERK;
- Sed converts all whitespace (delimiters) to a component number (;). ;
- Sed replaces all CLERK strings with ASSISTANT MANAGER;
To delete a line
Delete the NTH line from the display:
$ sed 'nd' file
Copy the code
Delete lines m through n from the display:
$ sed 'm,nd' file
Copy the code
Delete all rows with STR:
$ sed '/str/d' file
Copy the code
Delete all blank lines:
$ sed '/^$/d' file # re("^$") means a line without any word.
Copy the code
Delete rows from line 1 to row containing STR (including row containing STR) :
$ sed '1,/str/d' file
Copy the code
E.g. (1) E-mail and some applications display every line of information that begins with >, using the following sed command to do this:
$ sed '/^$/d; s/^/> /g' source >result
Copy the code
- The first command expression
/^$/d
To delete all blank lines, - The second command expression
s/^/>/
Replaces the start symbol with a greater than symbol and Spaces, - The last of the
>result
Saves the result of the sed command to the result file.
E.g. (2) delete all lines containing CAL at the same time as delete all empty lines, and change all string tie to fox, and save the result:
$ sed '/^$/d; /cal/d; s/tie/fox/g' source >result
Copy the code