Solved

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

Posted on 2016-08-29
6
58 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 11

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 6

Author Closing Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mac - rsync folders with / in name 5 60
VMWare 6 crashing 14 57
change time in cron 4 40
AWS CLI - Instances, Volumes & Tagging 2 20
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now