Solved

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

Posted on 2016-08-29
6
74 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 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

736 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