Search for shell scripts that replace text in multiple files in Linux

Source: Internet
Author: User

Use Find and SED

# First query the number (sed-N)

Find.-Name '*. htm' | xargs sed-n'/old/P'

# Replace (sed-I)

Find.-Name '*. htm' | xargs sed-I's/old/new/G'

Use grep and SED

Grep-l old *. htm | xargs sed-n "/old/P"
Sed-n'/old/P' grep-l old *. htm'

Use Find and Perl

Find.-Name '*. htm' | xargs Perl-pi-E's | old | new | G'

Refer:

Http://hi.baidu.com/msingle/blog/item/3b125b3fc0e1efe255e72375.html

Http://blog.csdn.net/zhangboyj/article/details/6215429

// Use shell to search for multiple files 
Find.-Type F | xargs grep flash
Note:
Find the files in the previous directory and Its subdirectories that contain Flash files.
//
Record the xargs command:
Xargs obtains the parameter list and one row in the standard input, and then sends them to another command in a large group (determined by the arg_max value of the host, this command is used as the xargs parameter.
How to view arg_max values:
Getconf arg_max
//
//
Search for multiple files and replace them with commands.
Grep flash $ (find.-Type F)
Note:
$ () Is a command replacement, with shell commands in parentheses. Shell executes the commands in parentheses and replaces $ (...) with the command output.
There are two methods to replace a command: $ () and two reverse quotation marks pair find.-Type F pair.

//Replace-sed in multiple files 
To ensure that the replacement should not be replaced, first find out in the file which will be replaced
Find-type F! -Path '*. SVN *' | xargs sed-n'/ookong INC/P'
Note:
1. Find first filters out the. SVN directory and Its subdirectories.
2. xargs sends parameters to the SED command
3. Sed performs a search and only prints the rows that meet the requirements.
//
// Replace
Find-type F! -Path '*. SVN *' | xargs sed-I's/ookong INC/ookong Ltd. co./G'
Note:
1. Sed's I option is directly replaced in the file, instead of printing the replacement result.
2.g replace all the matches in each line.
//
//
In addition, a Perl multi-file search program: ACK
-- Display the row number and file

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.