Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-08-29
6
Medium Priority
?
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

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 2000 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 5

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 5

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

688 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