Search and replace all files in Linux

Source: Internet
Author: User

Search and replace all files in Linux

You may often encounter this situation: Find all the files in a directory that contain a string, and replace the strings in these files with other strings. In this case, it is too troublesome to check and replace the files to be checked by the network. At this time, we should find a command to solve the problem.

1. grep command

Grep pattern file.txtcommand output the line matching pattern in the file.txt file to the standard output. This function can help us find the context of a string in the file, but it does not help us implement the next complex operation, so it is necessary to have a little understanding of some grep options.

The content of the test file is 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
To test this file, copy two more copies, rename the file and put it in the following directory structure:
$ tree ..├── a│   └── target1.txt├── target2.txt└── target.txt

Run the grep 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:
* It is a common bash wildcard, indicating all files in the current directory. Of course, you can also write a file name.
-R indicates recursive search, that is, searching for files in subdirectories of the current directory
-N indicates the row number.
-I case-insensitive
-L list matched file names
-L list unmatched file names
-W only matches the entire word, not a part of the string (for example, only 'man 'is matched, including signed man on both sides, such '. man. ', or' = man = 'Without 'Woman' or 'manly ')

2. Other Linux preparation skills 2.1 There are many ways to use command output as a parameter. Here two types are listed: ''and $ (). The following is an example.

$ echo `ls`a target2.txt target.txt t.tt$ echo $(ls)a target2.txt target.txt t.tt
For more information about how to replace the sed command with the 2.2 file, see the previous article. Here we only use the following:
Sed-I "s/old/new/g" file1.txt file2.txt can replace all old files in file *. txt with new. If new has nothing, it means to delete old.
3. concatenate the above skills so that only a few lines can be used to replace the specified strings in all files. Of course, you should also note that when using the sed command, you should note the escape when there are '/' characters, as shown below:
$ 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$
You may understand what I want to do. In fact, this is the case. A child shoes adds several static webpages to our online system. The hyperlinks used in webpages are all directly copied from the browser, including intra-site jumps. This is obviously unscientific. Although there was no problem at the time, but now our domain name has to be changed, the problem is that the jump links of these old domain names cannot be accessed. In order to solve this problem, I decided to delete all the domain names that are redirected from these sites to achieve normal jump. Because there are too many files, you have to use the above method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.