NO8 find combined with sed to find and replace enterprise case multi-methods and command summary!

sed #Replace eg: sed 'sed 's#Existing content#Changed content#g' oldboy.txt
                   s stands for replacement, g stands for global, sg is global replacement
                   ### separator, which can be replaced by any symbol, but Generally use # or /, @.
          1. sed command:
                 g: When used in combination with s, it means that the current line is globally matched and replaced.
                 s: often said find and replace, replace one string with another.
          Second, sed options:
                -e: allows multiple editing.
                -i: Modify the file content.

Example:
sed 's#oldgirl#jabbok#g' oldboy.txt
sed 's#existing content#changed content#g' oldboy.txt
 s###g: s stands for replacement, g stands for global, sg
             the ### separatorglobally
       
[root@localhost ~]# echo 'oldboy oldgirl' >oldboy.txt
[root@localhost ~]# cat oldboy.txt
oldboy oldgirl
[root@localhost ~]# sed 's#oldgirl#jabbok#g' oldboy.txt
oldboy jabbok
[root@localhost ~]# cat oldboy.txt        (only the output was changed, but the file content was not changed)
oldboy oldgirl
[root@localhost ~]# sed -i 's#oldgirl#jabbok#g' oldboy.txt (changed Add -i to the content)
[root@localhost ~]# cat oldboy.txt
oldboy jabbok


·Exercise:
[root@localhost data]# echo 'oldboy sinsen         (the echo command writes multiple lines) 
> oldgirl jabbok
> oldman aden'>>oldboy.txt
[root@localhost data]# cat oldboy.txt
oldboy sinsen
oldgirl jabbok
oldman aden 
[root@localhost data]# sed -e 's#old#new#g' oldboy.txt   (the contents of the file have not been changed at this time)
newboy sinsen
newgirl jabbok
newman aden
[root@localhost data]# cat oldboy.txt
oldboy sinsen
oldgirl jabbok
oldman aden
[root@localhost data]# sed -i 's#old#new#g' oldboy.txt (add -i to change the file content)
[root@localhost data]# cat oldboy.txt
newboy sinsen
newgirl jabbok
newman aden

 

 

· Question thirteen : put. Replace all the strings containing oldboy in all files with the extension .sh in the oldboy directory and its subdirectories with newboy. [Additional questions: extra points].
·Answer:
·Create test:
[root@localhost /]# mkdir -p oldboy/test/a/b/c/d
[root@localhost /]# tree /oldboy
/oldboy
└── test
    └── a
        └─ ─ b
            └── c
                └── d

5 directories, 0 files
[root@localhost /]# cd /oldboy
[root@localhost oldboy]# echo "oldboy" >test/del.sh
[root@localhost oldboy]# echo "oldboy" >test.sh
[root@localhost oldboy]# echo "oldboy" >t.sh
[root@localhost oldboy]# echo "oldboy" >.sh
[root@localhost oldboy]# tree
.
├── test
│   ├── a
│   │   └── b
│   │       └── c
│   │           └── d
│   └── del.sh
├── test.sh
└── t.sh

5 directories, 3 files
[root@localhost oldboy]# find /oldboy -type f
/oldboy/test/del.sh
/oldboy/test.sh
/oldboy/.sh
/oldboy/t.sh
[root@localhost oldboy]# touch ab c.txt         (create other type files)
[root@localhost oldboy]# find /oldboy -type f   (find all files)
/oldboy/test/del.sh
/oldboy/test.sh
/oldboy/.sh
/oldboy /t.sh
/oldboy/a
/oldboy/b
/oldboy/c.txt
[root@localhost oldboy]# find /oldboy -type f -name "*.sh"   (find files ending in .sh)
/oldboy/test /del.sh
/oldboy/test.sh
/oldboy/.sh
/oldboy/t.sh
[root@localhost oldboy]# find /oldboy -type f -name "*.sh"|xargs cat  (Looking at the contents of the .sh type file, xargs must be added to treat the data as an object file). oldboy oldboy oldboy oldboy [root@localhost oldboy]# find /oldboy -type f -name "*.sh"|cat  (without xargs, it will be used as a data stream directly) /oldboy/test/del.sh
/ oldboy/ test.sh /oldboy/.sh /oldboy/t.sh This effect is equivalent to: [root@localhost oldboy]# find /oldboy -type f -name "*.sh"|xargs  (first find the .sh type file) / oldboy/test/del.sh /oldboy/test.sh /oldboy/.sh /oldboy/t.sh [root@localhost oldboy]# cat /oldboy/test/del.sh /oldboy/test.sh /oldboy/. sh /oldboy/t.sh  ( look at the contents of the .sh file again) oldboy oldboy oldboy oldboy















·How to replace oldboy in .sh type file with newboy?
[root@localhost oldboy]# find /oldboy -type f -name "*.sh"|xargs sed 's#oldboy#newboy#g'
newboy
newboy
newboy
newboy
[root@localhost oldboy]# find /oldboy -type f - name "*.sh"|xargs sed -i 's#oldboy#newboy#g'  (after changing the output above, add -i to change the file content)
[root@localhost oldboy]# find /oldboy -type f -name " *.sh"|xargs cat   (View the modified content)
newboy 
newboy
newboy
newboy

Method 2:
[root@localhost oldboy]# sed -i 's#old#new#g' `find /oldboy -type f -name "*.sh"`
[root@localhost oldboy]# find /oldboy -type f -name "*.sh"|xargs cat
newboy
newboy
newboy
newboy
is equivalent to: sed -is#old#new#g /olddboy/test/del.sh /oldboy/test.sh /oldboy/.sh /oldboy/t. sh
The exam in this article seems to be simple. In fact, it has examined the basic command skills of a qualified operation and maintenance personnel, and it is not easy for most people to give more than 3 kinds of answers.

 

 


Summary:
1 mkdir # create directory -p recursively
2 touch # create file
3 ls # view list directory
4 cd # switch directory
5 echo # print
6 cp # copy directory -r recursive -a(dpr)
7 vi # notepad
8 head # Head. View the first N lines, the default first 10, -n number
9 rm # rm -f force -r directory
10 cat # check file content
11 remdir # delete empty directory
12 grep # filter -v exclude
13 find # find -type f -name "name" -mtime =7,7 -7
14 sed # CRUD -i modify -n cancel default output -e multiple edit
                          p s g
15 alias # modify and set alias, view
16 unlias # cancel alias
17 xargs # from standard output read in create execute command -n input number: grouping
18 awk # -F specifies the delimiter NR line number {print $1 $2}
19 seq # sequence prints the sequence 
                      -s specifies the delimiter
                      seq 10 By default, enter a carriage return after each number, which is a vertical row
                     [root@localhost oldboy]# seq -s "=" 10
                    1=2=3=4=5=6=7=8=9=10
20 pwd # Print the current directory
21 tree # Display the directory tree. Installation: yum install tree -y
22 tr        
23 tail # N lines after the tail view, 10 by default, -n number
24 vim # Complex editor, generally used for development (write shell, python)
25 mv # Move directory or file name , renamed

Commands that must be mastered:
man,touch,ls,mkdir,cp cd,rm,mv,echo,pwd,cat,alias,unalias,head,tail,tree,rmdir,find,xargs,awk,sed,grep,vi, vim, seq. . .

 

Guess you like

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