It's often the case that you look for all the files in a directory that contain a string, and replace the string in those files with a different string substitution. In this case, the network to check the file is more, one by one to check the replacement is too troublesome, this time, we should find a way to solve the problem of a command.
1. grep command
The default behavior of the grep pattern file.txt command is to output the line that matches the pattern in the File.txt file to the standard output. This feature helps us find the context of a string in a file, but it does not help us to do the next complex operation, so it is necessary to understand a little bit about the options of grep.
Now the contents of the test file 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 this file, we have copied two copies, renamed and placed in the following directory structure:
$ Tree: ├──a│└──target1.txt├──target2.txt└──target.txt
Execute 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:
* Is the General Bash wildcard, which represents all files in the current directory, and of course you can write a file name
-R is a recursive lookup, meaning that it also finds files in subdirectories of the current directory
-N is the line number displayed
-I ignores case
-l list matching file names
-l list file names that do not match
-W matches only the entire word, not part of the string (such as matching only ' man ', including the symbol '. Man ' on both sides of the man, or ' =man= ' instead of ' woman ' or ' manly ')
2, Linux Other preparation skills 2.1 The command output as a parameter way there are many kinds, here are listed two: "and $ (), below is an example."
$ echo ' ls ' a target2.txt target.txt t.tt$ echo $ (LS) a target2.txt target.txt t.tt
2.2 The use of the file replacement sedsed command is described in the previous article, where it is only used:
Sed-i "s/old/new/g" file1.txt File2.txt can change all the old in file*.txt to new. If new doesn't have anything, it means deleting old.
3, the above skill string up so that, only a few lines, you can complete all the files, specify a number of string substitution. Of course, also note that the use of SED commands, when there are '/' characters to be aware of the escape, 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" H ref= "/" >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 (139$ sed-i" s/http:\/\/www.asdf.net//g "$ (GREP-LR" http://www.asdf.net "*) $ sed-i" S/ht Tp:\/\/asdf.net//g "$ (GREP-LR" http://asdf.net "*) $ cat target.txt <a class=" Navbar-brand "href="/">panda S earch</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, perhaps you also understand what I want to do. In fact, this is the case, a child's shoes to our system on the line to add a few static pages, the Web page used in the hyperlinks are all directly copied from the browser, including the jump in the station. This is obviously unscientific, although there is no problem, but now our domain name to change, the problem comes, these old domain name of the jump link can not access. In order to eradicate this problem, I decided to these stations to jump the domain name part of all deleted, you can achieve the normal jump. Because there are too many files, we have to use the method above.
Linux find and Replace in all files