Replace the String in file without Opening a File in Linux 0 737

Replace the String in file without Opening a File in Linux 0 738

We usually come across the problem where we need to replace some string in multiple files and it will be really tedious to open each file and update the string.

Please remember you  have the idea of regular expression, shell command, sed or perl before reading this tutorial, else we will stuck in identify each of the command nomenclature and options being used.

I never like that and same expecting from you lazy buddy.

Let see what kind of problem we are facing in replacing the string.

Problem 1: Replace the string from the file.

Solution: You can just replace it used sed or perl command as both was installed default in Linux.  The commands are

sed  ‘s/foo/bar/g’ <filename>  or sed -i —  ‘s/foo/bar/g <filename’

or

perl -i -pe ‘s/foo/bar/g <filename>

Perl is much more promising as I feel but what your linux supports well you can check. In case it was not working just see the manual of these command to see what will be replaced with i (where -i switch with any version of sed has certain filesystem security implication)

Problem 2: Replace the similer type of string from multiple files.

Solution: Now the above command was just for one file, where there are the situation where we want to replace string from multiple file. In such cases we need to be very selective of the file?

See the following command that may work for your problem.

ls <filepattern> |xargs -i sed -i — ‘s/foo/bar/g’ {}

or

ls <filepattern> |xargs -i perl -i -pe ‘s/foo/bar/g’ {} 

The above command is used when there are the files in the specific directory in which we want to replace the string.

Now what to do if the file is in multiple directory. Hmm, thats little bit tricky.

find -name “filepattern” |xargs -i  sed -i — ‘s/foo/bar/g’ {}

or

find -name “filepattern” |xargs -i perl -i -pe ‘s/foo/bar/g’ {} 

Where filepattern is the regex of the file that you are looking for.  This tutorial is never meant to designed for beginner, so please bear with us, because if you don’t know sed , thats fine but you must know perl. Perl is very easy language to start with regular expression, text recognition, extraction from report.

Problem 3:  Replace the string in the file except some type of file.

Solution: There are the concern we usually face in the system administration where we just need to replace the string in certain set of files but not each of the file.  First we broken our problem, what kind of list of files we want to see that not contains specific files? Suppose we want to see the list of file except pdf files. What we will do? The command is simple.

ls <filespattern> |grep -v “\.pdf$”

The above command will list only files that are not having extension pdf, and you are intelligent people who knows how to exploit this command.

Once we have the listing of our file then let make the command with one of the above command we just learned.

ls <filespattern> |grep -v “\.pdf$” |xargs -i perl -i -pe ‘s/foo/bar/g’ {} 

Come on, you still need me to write every command, don’t do this, you have the view how you can exploit them. Best of luck. I end my tutorial here. You can write your comment and let see how much I can help you in solving your problems.

Previous ArticleNext Article
Hey guys, thanks for reading our posts. We are collecting news and information that required our attention and make us to thinks upon it. We make our blogs as much as informatory to support our needs. Hope we will be encouraged and have your comments and thoughts on maximum posts.

Send this to a friend