LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-12-2016, 03:02 AM   #1
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 10

Rep: Reputation: Disabled
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.
 
Old 05-12-2016, 03:36 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
sed itself can do the change and you need to write a loop on the list of files you have
 
Old 05-12-2016, 03:52 AM   #3
linuxuser06
LQ Newbie
 
Registered: May 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
So the following command should work?

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

Can you please help me with the loop part?
 
Old 05-12-2016, 03:55 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
"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.
 
Old 05-12-2016, 04:17 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by linuxuser06 View Post

Can you please help me with the loop part?
Look here for instance (example 5):
http://www.thegeekstuff.com/2011/07/...loop-examples/
 
Old 05-12-2016, 05:04 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.
 
Old 05-12-2016, 09:44 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GNU make best practices; single makefile, multiple programs or libs Enoch247 Linux - Software 4 06-01-2012 10:01 AM
Delete a single E-mail / multiple mbox files mgichoga Debian 0 02-18-2007 08:42 PM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 08:16 PM
Adding Multiple Files In A Single Gz File onacorpuscle Linux - Newbie 3 08-31-2006 03:18 AM
How To Merge multiple files into a single PDF ? kkempter Linux - Software 1 10-28-2005 01:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration