Solved

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

Posted on 2016-08-29
6
68 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

770 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