sed command collation in Linux

Keywords: Linux

Here is a simple knowledge combing of LInux sed command. On the one hand, in order to consolidate the knowledge they have learned, on the other hand, I hope to help other small partners who are learning Linux.

sed (English: stream editor) stream editor

Text files can be processed and edited according to script instructions. Its main purpose is to edit one or more files, simplify the repeated operation of files, write conversion programs, etc.

grammar

Sed [-hnV] [-e < script >] [-f < script file >] [text file]

parameter

  • [-hnV]

    • - h: Display help.
    • - n: Cancel the default output and display the results after script processing.
    • - V: Display version information.
  • [-e<script>]: Processing input text files with script specified in the option

  • [-f < script file >]: Processing input text files with script specified in the option

action

  • p: Print. Print the selected data. Tongshi runs with sed-n.
  • s: Find substitution, which can be used in conjunction with g to represent global matching substitution for the current row.
  • d: Delete,
  • C: Instead, strings can be followed by c, which can replace lines between N1 and n2.
  • A: Add a new string to the next line of the current line
  • i: Insert, insert a string to the previous line of the current line.
    Question: The number of 5-10 in ac.txt 0-30.
[root@Simile /]# sed -n '5,10'p ac.txt
5
6
7
8
9
10

Global substitution is achieved by. The If the - i parameter is not added, the output result will only change and the data of the file will not be modified.

[root@Simile /]# echo The new line > cd.txt
[root@Simile /]# cat cd.txt
The new line
[root@Simile /]# sed ' s#line#string#g' cd.txt
The new string
[root@Simile /]# cat cd.txt
The new line
[root@Simile /]# sed -i ' s#line#string#g' cd.txt
[root@Simile /]# cat cd.txt
The new string

It's not very standard to write about this today.
(Simile: If there are omissions and deviations in the text, the blogger will update and redistribute it as soon as possible. Thank you for your support and understanding.

Sitting on a topic with the old boy
Replace all of the files in the / test directory and its subdirectories ending with the extension. sh with all the strings containing Simile.

Method 1
Step 1: Establish test data.
Create / test directory and its subdirectory demo using mkdir command

[root@Simile /]# mkdir -p /test/demo

Use echo command to write the same data Simile in four files with different filenames suffixed with. sh

[root@Simile test]# echo "Simile" > demo/a.sh 
[root@Simile test]# echo "Simile" > b.sh 
[root@Simile test]# echo "Simile" > c.sh 
[root@Simile test]# echo "Simile" > .sh

Step 2: Find and view data
Use the find command to view the file data of the four. sh suffixes in the / test directory and its subdirectories

[root@Simile test]# find /test -type f -name "*.sh"
/test/b.sh
/test/c.sh
/test/.sh
/test/demo/a.sh

Using find command and pipeline xargs, use cat command to view data

[root@Simile test]# find /test -type f -name "*.sh" |xargs cat 
Simile
Simile
Simile
Simile

Part 3: Attempt to modify the output and eventually replace the data
Using find command and pipeline xargs, and then relying on sed s# # g to modify the output

[root@Simile test]# find /test -type f -name "*.sh" |xargs sed 's#Simile#Newline#g'
Newline
Newline
Newline
Newline
[root@Simile test]# find /test -type f -name "*.sh" |xargs cat 
Simile
Simile
Simile
Simile

Use sed-i parameter to replace the data and accomplish the task

[root@Simile test]# find /test -type f -name "*.sh" |xargs sed -i 's#Simile#Newline#g'
[root@Simile test]# find /test -type f -name "*.sh" |xargs cat 
Newline
Newline
Newline
Newline

Method 2
Note that the find section here is marked with inverted quotation marks on the Tab key.

[root@Simile test]# sed -i 's#Newline#Simile#g' `find /test -type f -name "*.sh"`
[root@Simile test]# find /test -type f -name "*.sh" |xargs cat 
Simile
Simile
Simile
Simile

Posted by hawnted on Thu, 27 Jun 2019 11:25:29 -0700