Concise usage of sed

This article aims to help newcomers to sed quickly become familiar with common uses of sed.
First, use the following file as the source file pets.txt:
This is my cat
  my cat's name is betty
This is my dog
  my dog's name is frank
This is my fish
  my fish's name is george
This is my goat
  my goat's name is adam


The replacement command s replaces
all my in the text with your: (where s represents the replacement command, and g represents the full line replacement)
$cat pets.txt | sed 's/my/your/g' >> pets1.txt


Replace line 1 of my text with your:
$cat pets.txt | sed '1s/my/your/g' >> pets1.txt


Replace lines 1-3 of my text with your:
$cat pets.txt | sed '1,3s/my/your/g' >> pets1.txt


replace the first my of each line
$cat pets.txt | sed 's/my/your/1' >> pets1.txt


replace the 2nd my of each line
$cat pets.txt | sed 's/my/your/2' >> pets1.txt


Replace the 3rd my of each line and after
$cat pets.txt | sed 's/my/your/3g' >> pets1.txt



Add # at the beginning of each line in pets1.txt
cat pets1.txt | sed 's/^/#/g' >> pets2.txt


Add ---- at the end of each line in pets2.txt
cat pets2.txt | sed 's/$/----/g' >> pets3.txt


In the above two examples, ^ represents the beginning of each line, $ represents the end of each line, and by the way some basic things about regular expressions:

^ indicates the beginning of a line. Such as: /^#/ matches starting with #.
$ indicates the end of a line. For example: /}$/ matches ending with }.
\< indicates the beginning of a word. For example, \<abc means words starting with abc.
\> indicates the end of a word. For example, abc\> means words ending in abc.
. means any single character.
* Indicates that a character occurs 0 or more times.
[ ] Character set. For example: [abc] means matching a or b or c, and [a-zA-Z] means matching all 26 characters. If there is ^ in it, it means reverse, such as [^a] means non-a character


Regular expressions are awesome, such as test.html:
<b>This</b> is what <span style="text-decoration: underline;">I</span> meant.

To remove the label: (where [^>]* represents 0 or more non-> characters)
cat test.html | sed 's/<[^>]*>//g'


The prefetch command N

appends the even lines of the text pets.txt to the odd lines:
cat pets.txt | sed 'N; s/\n/,/g' >> mypets.txt

The output is purple:
This is my cat,  my cat's name is betty
This is my dog,  my dog's name is frank
This is my fish,  my fish's name is george
This is my goat,  my goat's name is adam


An example of using parentheses to match: (The string matched by the regular expression enclosed in parentheses can be used as a variable, and sed uses \1, \2...) For
example:
cat mypets.txt | sed 's/This is my \([^,]*\),  my .* \(.*\)/\1:\2/g'

The result is as follows:
cat:betty
dog:frank
fish:george
goat:adam

About this command need to be introduced, [^,]* represents 0 or more non-comma characters, .* represents 0 or more arbitrary characters. To use brackets to indicate bracket matching, you need to use the escape character \ to


insert the commands i and a
. Take mypets.txt as an example, insert a line before the first line:
cat mypets.txt | sed "1i This is my pig,  my pig's name is bajie"


Take mypets.txt as an example, insert a line after the first line:
cat mypets.txt | sed "1a This is my pig,  my pig's name is bajie"


We can use a match to add text and insert a line after the dog:
cat mypets.txt | sed "/dog/a This is my pig,  my pig's name is bajie"


The whole line replacement command c
takes mypets.txt as an example, and replaces the second line:
cat mypets.txt | sed "2c This is my pig,  my pig's name is bajie"


Replace the line where dog is located
cat mypets.txt | sed "/dog/c This is my pig,  my pig's name is bajie"


The entire line deletion command c

takes mypets.txt as an example, and deletes the second line:
cat mypets.txt | sed '2d'


Take mypets.txt as an example, replace the line where dog is located:
cat mypets.txt | sed '/dog/d'


Take mypets.txt as an example, delete the second line and all subsequent lines:
cat mypets.txt | sed '2,$d'


Print the entire line of the command p to

find the line in /etc/passwd that contains root:
cat /etc/passwd | sed -n '/root/p'
The above command is equivalent to
grep 'root' /etc/passwd


In addition, to overwrite the original file use -i, for example:
$cat pets.txt | sed 's/my/your/g' >> pets1.txt

The writing method to overwrite the original file is:
sed -i 's/my/your/g' pets.txt


The above content is forwarded and sorted from http://coolshell.cn/articles/9104.html#more-9104

grep, sed, awk and become the three linux warriors, the three of them happen to be a set of processes for processing characters, grep is used to find the target characters, sed is used to edit characters, and awk is used to select and analyze characters.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326512639&siteId=291194637