Solved

edit multiple files in several directories with the same text in Linux

Posted on 2016-08-29
6
73 Views
Last Modified: 2016-09-10
Hi everyone.
I have 6 files that are almost identical to each other but they are in different folders
so lets say I have

/home/functions/ver11/file.conf
/home/functions/ver12/file.conf
/home/functions/ver30/file.conf
/home/functions/ver54/file.conf

all files have a line that reads: "Depricated funcstions="
Currently, this line is blank after the "=" in all the files.
assuming I need to enter the same text to all files (For example I want all files to be edited so that the lines now reads:
"Depricated dunctions=foo, bar"
but the row number in which the line appears is different for each file.
is there a way to achieve that for all file.conf files in /home/functions/verXX/file.conf?
0
Comment
Question by:David Sankovsky
6 Comments
 
LVL 3

Expert Comment

by:jessbruffett
ID: 41774414
you can use sed to do this. i would recommend writing a script to do it. this page pretty much explains the command in terms of how it works and proper syntax. https://www.gnu.org/software/sed/manual/sed.html

the script is important because sed will read the files you tell it to, make the changes and then output the changed data to the location you tell it. ive found its best to send them to a temp location then move them into production. also you can use the script to make backups of the originals in case something happens and you need them.
0
 
LVL 1

Accepted Solution

by:
Chris Marx earned 500 total points
ID: 41774440
you could use a perl one line script to edit this files i.e.
perl -pi -e 's/Depricated funcstions=\K$/foo, bar/' /home/functions/ver??/file.conf

Open in new window


you might make backups too, giving
perl -pi.backup -e 's/Depricated funcstions=\K$/foo, bar/' /home/functions/ver??/file.conf

Open in new window

0
 
LVL 4

Expert Comment

by:Abhimanyu Suri
ID: 41776486
:/home>cat file.conf
Depricated functions=

:/home>ls -ltrh
drwxr-xr-x 2 suri suri 4.0K Aug 30 10:52 ver1
drwxr-xr-x 2 suri suri 4.0K Aug 30 10:53 ver2

:/home>cp file.conf ver1/
:/home>cp file.conf ver2/

:/home>ls -ltr ver1
file.conf

:/home>ls -ltr ver2
file.conf

:/home>cat ver1/file.conf
Depricated functions=
:/home>cat ver2/file.conf
Depricated functions=

:/home>sed -i'' 's/Depricated functions=/Depricated functions=sed/g' /home/ver*/file.conf

:/home>cat ver1/file.conf
Depricated functions=sed
:/home>cat ver2/file.conf
Depricated functions=sed


Please note that actual files will be changed by this approach and all lines with string "Depricated functions"  , please back up files before testing
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 12

Expert Comment

by:tel2
ID: 41777290
Not bad, Abhimanyu, but here are some tips for possibly improving your answer slightly:

Looking at this command:
   sed -i'' 's/Depricated functions=/Depricated functions=sed/g' /home/ver*/file.conf

The -i switch doesn't need '' after it.
Your comment about backing up files first could have been handled by providing an alternative solution which did that automatically, e.g.
    sed -i.bak ...

You don't need to repeat the "Depricated functions=" match.  An "&" can be used on the right hand side of the substitution to repeat the entire left hand side.

sed's "/g" modifier is required only if you want to replace multiple matches per line, so shouldn't be needed here.

You are already in the /home directory, so you don't need to start the path with /home (but I know there may be reasons for doing so).  Also, the asker's files are in /home/functions/ver...

So, your command could be reduced to something like this:
    sed -i 's/Depricated functions=/&sed/' ver*/file.conf
Or if you want to create backup files and name them file.conf.bak:
    sed -i.bak 's/Depricated functions=/&sed/' ver*/file.conf


Also, your use of "ls -ltrh" and "ls -ltr" could probably just be simplified to "ls -l" in cases like this.  I don't think the extra switches are adding value here.

But your code does appear to work, and well done.
0
 
LVL 4

Expert Comment

by:Abhimanyu Suri
ID: 41777837
As always, thanks for the tips @tel2
0
 
LVL 7

Author Closing Comment

by:David Sankovsky
ID: 41792913
great one liner,
Worked like magic. Thank you
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question