"Go" Linux batch update file content + "original" Escape when the problem

Source: Internet
Author: User

program development, you may often encounter the situation of bulk replacement of file content, if you are using Linux, then congratulations, you can use three ways to find and replace.

Method One: Find-name ' File.log ' |  Xargs perl-pi-e ' s|string1|string2|g ' here uses the Perl language, using-e plus a piece of code, which in bulk will replace string1 in the File.log file in the current directory and all subdirectories into string2 String supports regular expression method two: Sed-i "s/string1/string2/g" ' grep string1-rl/path ' here uses the SED command to replace string1 in all files under the/path directory and its subdirectories with the St   Ring2 This command, may not be very supportive of returning multiple files, not recommended! Method Three: Grep-i "string1"-r/path | Awk-f: ' {print $} ' | Sort | Uniq | Xargs sed-i ' s/string1/string2/g ' has been used here Shellcommand, find the file first, then use awk to split (to: Slice), and then replace!     Note:  grep can use regular, or you can use \ to escape some special characters, such as "et   sed-i ' s/\" localhost\ "/\" 10.2.2.2\ "/g"/home/ My.conf-------------------------------------above is the original posts, address: http://www.51testing.com/html/93/316693-815340. HTML-------------------------------------------------------  boss found a SQL injection vulnerability, let me batch update all files of the same name this place, where ("id=" ". $rec." ") Change to where (array ("number" = $rec)) because I am also just getting started Linux, so immediately Baidu method, first used sed-i "s/string1/string2/g" ' grep string1-rl/path '   Sed-i "S/where (\" id= \ "\" \ "\ $rec. \" \ ")/where (Array (\" id\ "=>\ $rec))/g" ' grep  -rl/home '   Test, execute, success, The heart cheerfully in the official server executed, then quickly off work, closed the screen retreat. The next morning to open the screen to see, I wipe, not finished, a think indeed, so many files screen display also laborious, and then Baidu, because are the same name of the file, so this time with  find-name ' File.log ' | Xargs perl-pi-e ' S|string1|string2|g ' This method, the middle because the escape fee for a long time,  find/home/-name ' xxxx.php ' | Xargs perl-pi-e "s|where\ (\" id= "\" \ "\" \ "\) |where\ (Array (\" id\ "=>\ $rec \) \) |g"  -> error first wrote \ $rec Then it is unsuccessful, there is no way only Baidu escaped, found a lot of posts are some in order to escape the use of 5,678 to escape his own, suddenly, the original \$ escaped or $rec is a variable,  find/home/-name ' xxxx.php ' | Xargs perl-pi-e "s|where\ (\" id= "\". \\\ $rec. \ "\" \) |where\ (Array (\ "id\" =>\\\ $rec \) \) |g "correctly because the \\\$ is escaped from the double quotes first, From the double quotation mark out when it is already \$, and then the command execution and then escaped into the $rec, which finally good, test success, executed half a minute, all replaced,

"Go" Linux batch update file content + "original" Escape when the problem

Related Article

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.