Search and replace on multiple files
Making some kind of text search and replace operations on a single or multiple files is a trivial task for every Linux system admin or even normal user. There are different ways for this, we just want to describe two of them.
If you already familiar with perl and know a little regular expression syntax, you can use one-line embedded perl usage:
perl -pi -e 's/search/replacement/g;' *.txt
or combine with find and xargs:
find -name "*.html" | \
xargs perl -pi -e 's/search/replacement/g;'
You can use all the power of regular expressions and perl to further improvements.
If you found above usage a little complex, there is also a simple utility named replace which comes with mysql-client package. Common usage of replace as follow:
replace from to from to from to ... -- files
You can replace multiple texts to their replacements same time, when you finish writing your from-to replacements, you have to put a -- characters and after that you have to provide file or file list (like *.txt). Here a simple example:
replace mkae make meka make amke make -- *.sh
Above commands corrects all three typing error of "make" in the files end with .sh.
- 0 Comment
- Murat Demirten
- 25 Jun 2008, 12:34
-
You must be login first or sign-up for an account to post comments.
Maybe you should look at these also:
USERBOX
CATEGORIES
MOST READED TODAY
- Creating splitted rar archive files
- Converting MAC dfonts to TrueType
- XFS Filesystem has duplicate UUID problem
- dropbear rsa key problem
- Network interface status with mii-tool
- Recovering Root Password
- Recovering mysql root password
- Changing time in Linux
- Listing files by size
- Passwordless sudo setup
LAST ADDED
- Routing packets back from incoming interface
- Users with /bin/false shell to login on vsftpd
- Redirecting TCP ports with redir
- Assembly code of a C code
- Network interface status with mii-tool
- dropbear rsa key problem
- Displaying disk io statistics
- Disabling ssh password authentication
- Creating splitted rar archive files
- Creating self-extracting zip archives for both Linux and Windows
