Linux finds and replaces

  

in all files. It is often the case to find all files in a directory that contain a string and replace the string in those files with another string. Replace it. In this case, the network has to check a lot of files, it is too much trouble to check and replace one by one. At this time, we should find a way to solve the problem with one command.

1, grep command

The default behavior of the grep pattern file.txt command is to output the line matching the pattern in the file.txt file to the standard output. This feature helps us find the context in which a string appears in the file, but it does not help us to implement the next complex operation, so it is necessary to have a little understanding of some of grep's options.

The contents of the test file now are as follows:

$ cat target.txt <a class="navbar-brand" href="http://yyy.xxx.edu.cn /">Panda Search</a>text-decoration: none;" href="http://www.asdf.net/scholar/xindong_wu.html">Xindong" href="http://asdf.net/scholar/Federi <a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a> href="http ://www.asdf.net/scholar/Gong-Qing_Wu.html">href="http://asdf.net/scholar/Federico_Bocardi.html">Federico occardi</a></li> <span class="Apple-converted-space"> </span><li style="display: inline-block; padding-right: 5px; padding-left: 5px;">< ;a style="color: rgb(66, 139 In order to test we will copy this file, copy two copies, rename it and put it in the following directory structure: 
$ tree ..├── a│ └ ── target1.txt├── target2.txt└── target.txt

execute gr Ep command:

$ grep -rn "http://yyy.xxx.edu.cn" * a/target1.txt:1:<a class="navbar-brand" href=" ;http://yyy.xxx.edu.cn/">Panda Search</a>a/target1.txt:5: <a class="navbar-brand" href="http://Yyy.xxx.edu.cn/">Panda Search</a>target2.txt:1:<a class="navbar-brand" href="http://yyy.xxx.edu.cn /">Panda Search</a>target2.txt:5: <a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search< ;/a>target.txt:1:<a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a>target.txt :5: <a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a>grep provides the following options: * is normal Bash wildcard, which means all files in the current directory. Of course, you can also write a file name -r is a recursive lookup, which means that the file in the subdirectory of the current directory is also found. Line number -i ignores case -l lists matching file name -L lists mismatched file name -w matches only the entire word, not part of the string (such as only matching ‘man’, including both sides of man Signed such as ‘.man.’, or ‘=man=’not including ‘ woman ’ or ‘ manly ’)

2, Linux other preparatory skills 2.1 will command output There are many ways to parameterize, here are two types: `` and $(). Here is an example.

$ echo `ls`a target2.txt target.txt t.tt$ echo $(ls)a target2.txt target.txt t.tt2.2 File replacement sed sed command use see previous article Here, I only use the place: 
sed -i "s/old/new/g" file1.txt file2.txt You can replace all old in file*.txt with new. If new doesn't have anything, it means deleting old. 3, the above skills are stringed together, just a few lines, you can complete all the files, specify the replacement of several strings. Of course, also pay attention to the use of the sed command, when you have the '/' character, you should pay attention to escaping, as follows: 
$ sed -i "s/http:\\/\\/yyy.xxx. Edu.cn//g" $(grep -lr "http://yyy.xxx.edu.cn" *)$ cat target.txt <a class="navbar-brand" href="/" ;>Panda Search</a>text-decoration: none;" href="http://www.asdf.net/scholar/xindong_wu.html">Xindong" href="http://asdf .net/scholar/Federi <a class="navbar-brand" href="/">Panda Search</a> href="http://www.asdf.net/scholar/Gong- Qing_Wu.html">href="http://asdf.net/scholar/Federico_Bocardi.html">Federico occardi</a></li><span class="Apple-converted-space" > </span><li style="display: inline-block; padding-right: 5px; padding-left: 5px;"><a style="color: rgb(66, 139 $ sed -i "s/http:\\/\\/www.asdf.net//g" $(grep -lr "http://www.asdf.net" *)$ sed - i "s/http:\\/\\/asdf.net//g" $(grep -lr "http://asdf.net" *) $ cat target.txt <a class="navbar-brand" ; href="/">Panda Search</a>text-decoration: none;" href="/scholar/xindong_wu.html">Xindong" href="/scholar/Federi <a Class="navbar-brand" href="/">Panda Search</a> href="/scholar/Gong-Qing_Wu.html">href="/scholar/Federico_Bocardi.html"> ;Federico occardi</a></li><span class="Apple-converted-space"> </span><li style="display: inline-block; padding-right: 5px ; padding-left: 5px;"><a style="color: rgb(66, 139$ See here, maybe you also understand what I am going to do. In fact, this is the case. A children's shoe adds several static web pages to our online system. The hyperlinks used in the web pages are all copied directly from the browser, including the jumps in the station. This is obviously unscientific, although there was no problem at the time, but now our domain name has to be changed, the problem is coming, and the jump links of these old domain names cannot be accessed. In order to cure this problem, I decided to delete all the domain names in these stations, and I can achieve a normal jump. Due to too many files, I had to use the above method.
Copyright © Windows knowledge All Rights Reserved