Shell script — detailed explanation of SED usage

Time:2021-9-19

Sed reads the contents of the file line by line when processing text. When it reads a matching line, it operates according to the instruction. If it does not match, it skips

Sed is a powerful non interactive streaming text editor under Linux. It can add, delete, modify and query text files. It supports matching text content by line, field and rule. It is flexible and convenient, especially suitable for editing large files. This article mainly introduces some basic uses of SED, and demonstrates the use of SED through shell script.

1. The use method of sed. There are two kinds of syntax for calling sed command:

1、 Specify the SED instruction on the command line to process text: sed + option   ‘ Directive ‘file

2、 First save the SED instruction to a file and call the file as a parameter: sed    + option   – f   File containing sed directive   file

Common options for sed:

-r: Using extended regular expressions

-e: It tells sed to interpret the next parameter as a sed instruction. The – e option is only required when multiple sed instructions are given on the command line

-f: Followed by a file that holds the SED directive

-i: Modify the content directly. If – I is not added, it is only preview by default, and the file will not be actually modified

-n: Cancel the default output, sed will output all text contents by default, and only the processed lines will be displayed after using the – n parameter

Edit commands in sed:

a: Add   Insert content after matching line

c: Change   Change the contents of the matching line

i: Insert   Insert content before matching line

d: Delete   Delete matching content

s: Replace   Replace the matching content

p: Print   Print out the matching content, usually with the – N option and

=: used to print the line number of the matched line

n: Read the next line and automatically jump to the next line when n is encountered

r. W: read and write edit commands, R for reading content into a file, and W for writing matching content to a file

2. Sed command example:

Example 1: adding or inserting rows into a file

Sed '3A Hello' 1.txt # adds Hello after the third line, and 3 represents the line number

Sed '/ 123 / ahello' 1.txt # add Hello after the content 123. If there are multiple lines including 123 in the file, it will be added after each line

Sed '$ahello' 1.txt # add hello on the last line

Sed '3ihello' 1.txt # insert hello before the third line

Sed '/ 123 / ihello' 1.txt # inserts hello before the line containing 123. If multiple lines contain 123, hello will be inserted before each line containing 123

Sed '$ihello' 1.txt # insert hello before the last line

Example 2: changing a specified line in a file

Sed '1cello' 1.txt # replaces the first line of file 1.txt with Hello

Sed '/ 123 / Chello' 1.txt # replace the line containing 123 with Hello

Sed '$Chello' 1.txt # replaces the last line with Hello

Example 3: delete a line in a file

Sed '4D' 1.txt # delete the fourth line

Sed '1 ~ 2D' 1.txt # from the first line, delete every two lines, that is, delete odd lines

Sed '1,2d' 1.txt # delete lines 1 ~ 2

sed '1,2! D '1.txt # delete all lines except 1 ~ 2

Sed '$d' 1.txt # delete last line

Sed '/ 123 / D' 1.txt # delete rows matching 123

Sed '/ 123 /, $d' 1.txt # delete from the line matching 123 to the last line

Sed '/ 123 /, + 1D' 1.txt # delete the line matching 123 and its subsequent line

Sed '/ ^ $/ D' 1.txt # delete empty lines

sed '/123\|abc/! D '1.txt # delete the row that does not match 123 or ABC, / 123 \ | ABC / means match 123 or ABC,! Indicates negation

Sed '1,3 {/ 123 / D}' 1.txt # delete the line with matching content 123 in 1 ~ 3 lines, 1,3 means matching 1 ~ 3 lines, and {/ 123 / D} means deleting the line with matching 123

Example 4: replacing content in a file

Sed's / 123 / hello / '1.txt # replaces 123 in the file with Hello. By default, only the first 123 in each line is replaced

Sed's / 123 / hello / g '1.txt # replaces all 123 in the text with Hello

Sed's / 123 / hello / 2 '1.txt # replace the second matching 123 in each line with Hello

Sed - n's / 123 / hello / GPW 2. TXT '1.txt # replace all matching 123 in each line with Hello, and write the replaced content to 2.txt

Sed '/ # / S /,. * // g' 1.txt # match the line with # sign, replace all the contents after the comma in the matching line, and be empty (,. *) indicates the contents after the comma

Sed's /.. $// g '1.txt # replaces the last two characters in each line as empty, each dot represents a character, and $means matching, and the end (.. $) means matching the last two characters

Sed's / ^ #. * // '1.txt # replace the line starting with # in the 1.txt file with an empty line, that is, the line of the comment (^ #) indicates that the match starts with #, (. *) represents all contents

sed 's/^#.*//;/^$/ D '1.txt # first replace the blank lines of all comments in the 1.txt file, and then delete the blank lines. The replacement and deletion operations are separated by semicolons

Sed's / ^ [0-9] / (&) / '1.txt # add a small bracket (^ [0-9]) to the number at the beginning of each line, indicating that the beginning of the line is a number, and the & symbol represents the matching content
#Or
Sed's / \ (^ [0-9] \) / (\ 1) / '1.txt # replacing the special characters on the left requires key escape, and the right does not need escape, \ 1 represents the matching content

Sed's / $/ & 'haha' / '1.txt # add the "haha" field after each line of the 1.txt file

Example 5: printing lines in a file

Sed - n '3P' 1.txt # prints the third line in the file

Sed - n '2 ~ 2p' 1.txt # from the second line, print a line every two lines, and 2 after the tilde indicates the step size

Sed - n '$p' 1.txt # prints the last line of the file

Sed - n '1,3p' 1.txt # print 1 to 3 lines

Sed - n '3, $p' 1.txt # print from line 3 to the last line

Sed - n '/ you / P' 1.txt # read the file line by line and print the lines matching you

Sed - n '/ Bob /, 3P' 1.txt # read the file line by line and print the contents from the line matching Bob to line 3

Sed - n '/ you /, 3P' 1.txt # print lines matching you to line 3, and also print all subsequent lines matching you

Sed - n '1, / too / P' 1.txt # print the first line to match too
Sed - n '3, / you / P' 1.txt # only print the third line to the line matching you

Sed - n '/ too /, $p' 1.txt # prints the contents from the line matching too to the last line

Sed - n '/ too /, + 1p' 1.txt # print the line matching too and its subsequent line. If multiple lines match too, each matching line will print one more line backward

Sed - n '/ Bob /, / too / P' 1.txt # print the line from the matching content Bob to the matching content too

Example 6: print the line number of the file

Sed - n "$=" 1.txt # print the line number of the last line of the 1.txt file (that is, how many lines the file has, similar to WC - L function)

Sed - n '/ error / =' 1.txt # print the line number of the line matching error
Sed - n '/ error / {=; P}' 1.txt # print the line number and content of the line matching error (it can be used to view the line with error in the log and its content)

Example 7: reading content from a file

Sed 'R 2. TXT' 1.txt # read the contents of the file 2.txt into 1.txt, and the contents of 2.txt will be read after each line in 1.txt

Sed '3R 2. TXT' 1.txt # inserts the contents of file 2.txt after line 3 of 1.txt (which can be used to insert contents into the file)

Sed '/ 245 / R 2. TXT' 1.txt # inserts the contents of file 2.txt after the line matching 245. If there are multiple lines matching 456 in 1.txt, it will be inserted after each line

Sed '$R 2. TXT' 1.txt # inserts the contents of 2.txt in the last line of 1.txt

Example 8: writing content to a file

Sed - n 'W 2. TXT' 1.txt # writes the contents of the 1.txt file to the 2.txt file. If the 2.txt file does not exist, it will be created. If the 2.txt file exists, it will overwrite the previous contents

Sed - n '2W 2. TXT' 1.txt # writes line 2 of file 1.txt to file 2.txt

Sed - N - E '1W 2. TXT' - E '$W 2. TXT' 1.txt # writes the first and last lines of 1.txt to 2.txt
Sed - N - E '1W 2. TXT' - E '$w 3. TXT' 1.txt # writes the first and last lines of 1.txt to 2.txt and 3.txt, respectively

Sed - n '/ ABC \ |123 / W 2. TXT' 1. TXT # writes the contents of the line matching ABC or 123 in 1.txt to 2.txt

Sed - n '/ 666 /, $W 2. TXT' 1.txt # writes the contents of 1.txt from the line matching 666 to the last line to 2.txt

Sed - n '/ XYZ /, + 2W 2. TXT' 1.txt # writes the contents of the line matching XYZ and the next two lines in 1.txt to 2.txt

Example 9: use of sed in shell script

Example 1: replacing the contents of a file

#!/bin/bash
if [ $# -ne 3 ]; Then # judgment parameter number
 Echo "usage: $0 old part new part filename" # output script usage
 exit
fi
Sed - I "s # $1 # $2 #" $3 # replaces the old content. When $1 and $2 contain "/", the delimiter in the replacement instruction needs to be replaced with other symbols

Example 2: delete blank lines in a file

#!/bin/bash
if [ ! -f $1 ]; Then # determines whether the parameter is a file and exists
 echo "$0 is not a file"
 exit
fi
Sed - I "/ ^ $/ D" $1 # delete blank lines

Example 3: formatting text content

#!/bin/bash
A ='s / ^ * > // # defines a variable a to save the SED instruction,'s / ^ * > // ': means to match the line starting with 0 or more spaces followed by a' > 'sign, and replace the matching content
S / \ t * // #'s / \ t * // ': it means to match the line starting with 0 or more tabs, and replace the matching content
S / ^ > // #'s / ^ > // ': it means to match the line starting with' > ', and replace the matching content
S / ^ * // '#'s / ^ * //': indicates that the line starting with 0 or more spaces is matched, and the matching content is replaced
#echo $a
Sed "$a" $1 # formats the text file given by the user

Practical script: batch change the file suffix in the current directory:

Example 1:

#!/bin/bash
if [ $# -ne 2 ]; Then # determines the user's input. If the number of parameters is not 2, the script usage will be printed
 echo "Usage:$0 + old-file new-file"
 exit
fi
For I in * $1 * # traverses the file containing the parameters given by the user
do
 if [ -f $i ];then
 Iname = ` basename $I ` # get file name
 Newname = ` echo $Iname | sed - e "s / $1 / $2 / g" ` # replace the file name and assign it to the new variable
 MV $Iname $newname # renames the file
 fi
done
exit 666

Example 2:

#!/bin/bash
Read - P "input the old file:" old # prompts the user to enter the file suffix to replace
read -p "input the new file:" new
[- Z $old] | [- Z $new] & & echo "error" & & exit # judge whether the user has input. If not, how to print error and exit
for file in `ls *.$old`
do
 if [ -f $file ];then
 Newfile = ${file% $old} # end the file
 MV $file ${newfile} $new # file rename
 fi
done

Example 3:

#!/bin/bash

if [ $# -ne 2 ]; Then # determines whether the number of location variables is 2
 echo "Usage:$0 old-file new-file"
 exit
fi
For file in ` LS ` # traverses the file in the current directory
do
 if [[ $file =~ $1$ ]]; Then # performs regular matching on the location variable $1 given by the user, $1 $means matching the file ending with the value of variable $1
 Echo $file # outputs the match to the screen for confirmation
 New = ${file% $1} # de tail the file, remove the file suffix, retain the file name, and assign the file name to the variable new   
 MV $file ${new} $2 # renames the matching file to: filename + new suffix
 fi

done

Example 4: using sed to match the IP address in the file


sed -nr '/([0-9]{1,3}\.){3}([0-9]{1,3})/p' 1.txt

This is the end of this article about the usage of shell script sed. For more shell sed content, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]