LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to make a single change to multiple files (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-a-single-change-to-multiple-files-4175579587/)

linuxuser06 05-12-2016 03:02 AM

How to make a single change to multiple files
 
I have to make a single change to 250 files under same directory.
All the 250 lines are sh files which contain a version number. I have to update this version number in all 250 files.

Can sed command be used to do it in a shot or do I need to write a script for this?

Please help.

OS : Linux- RHEL 7

Thanks in advance.

pan64 05-12-2016 03:36 AM

sed itself can do the change and you need to write a loop on the list of files you have

linuxuser06 05-12-2016 03:52 AM

So the following command should work?

sed -i 's/5.9/5.10/g' filename

Can you please help me with the loop part?

syg00 05-12-2016 03:55 AM

"filename" can be a glob.
Create a test directory with a few files. Easy to test and make sure you don't change things you didn't mean to.

HMW 05-12-2016 04:17 AM

Quote:

Originally Posted by linuxuser06 (Post 5544280)

Can you please help me with the loop part?

Look here for instance (example 5):
http://www.thegeekstuff.com/2011/07/...loop-examples/

Turbocapitalist 05-12-2016 05:04 AM

sed is good. perl works too. You can also do it with a perl one-liner:

Code:

perl -pi.orig -e 's/old/new/' *.txt
That swaps 'old' for 'new' in all files with a name ending with .txt in the current directory, and makes backup copies of the unchanged originals by appending .orig to the name.

s/// takes all the arguments that perl supports and can be written as s||| or s### if you need to use slashes.

grail 05-12-2016 09:44 AM

You need to be cautious with your current regex as it will match more than intended:
Code:

5.9
will match
5.9
519
5a9
5B9
5&9
...

So if you have any character between a 5 and a 9 elsewhere in your files you will get more changes than required.

Try to look to see what makes the line unique, like does the line contain the word 'version' (capitalised or otherwise).


All times are GMT -5. The time now is 12:47 PM.