[SOLVED] Mass Editing files with 'sed', replacing a string of text with different lines
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Mass Editing files with 'sed', replacing a string of text with different lines
Let's say I have the following files:
/etc/dir1/file.txt
/etc/dir2/file.txt
/etc/dir3/file.txt
... all the way up to dir100 (100 directories), every directory has file.txt
And I have the following text file in /root/list.txt In list.txt, I have 100 lines, each line with a different string of text.
In each "file.txt, they all have the string of text, "word1" on it
How would I use sed (or something similar) to replace the word "word1" in every file.txt, with 1 line in list.txt? Each line in list.txt, is only to be used once.
So for example, replace "word1" in /etc/dir1/file.txt with the first line in /root/list.txt, and replace "word1" /etc/dir2/file.txt with the second line in /root/list.txt and so on, all the way up to 100.
I greatly appreciate any help and assistance here as "sed" is not my strong point.
for ((i=1; i<=100; i++))
do
newword=$(sed -n "${i}p" /root/list.txt)
sed "s/word1/$newword/g" /etc/dir$i/file.txt
done
For the future, please show what you've done and where you're stuck when you ask for help. Generally, people here are more than happy to help, but not do it for you. Just a comment.
Bear in mind, the above script will not change anything permanently, if that's what you want, just add the -i flag to sed.
Please remember to back things up before you test commands on important files.
for ((i=1; i<=100; i++))
do
newword=$(sed -n "${i}p" /root/list.txt)
sed "s/word1/$newword/g" /etc/dir$i/file.txt
done
For the future, please show what you've done and where you're stuck when you ask for help. Generally, people here are more than happy to help, but not do it for you. Just a comment.
Bear in mind, the above script will not change anything permanently, if that's what you want, just add the -i flag to sed.
Please remember to back things up before you test commands on important files.
Thanks. I am actually an entry level Linux systems technician (learning new things everyday), and look forward to contributing throughout the forums in the best manner that I can as I learn.
I'm getting the following error when trying your script:
edit.sh: 1: edit.sh: Syntax error: Bad for loop variable
Thanks. I am actually an entry level Linux systems technician (learning new things everyday), and look forward to contributing throughout the forums in the best manner that I can as I learn.
I'm getting the following error when trying your script:
edit.sh: 1: edit.sh: Syntax error: Bad for loop variable
root@dev:~# cat edit.sh
#!/bin/bash
for ((i=1; i<=100; i++))
do
newword=$(sed -n "${i}p" /root/list.txt)
sed "s/word1/$newword/g" /etc/dir$i/file.txt
done
root@dev:~# sh edit.sh
edit.sh: 2: edit.sh: Syntax error: Bad for loop variable
root@dev:~#
If sed is not your strong point, why don't you use a tool that you are good at?
Code:
for ((i=1; i<=100; i++))
do
newword=$(sed -n "${i}p" /root/list.txt)
sed "s/word1/$newword/g" /etc/dir$i/file.txt
done
I would replace the first sed command with a simple read. Also, the code has to handle the case that newword contains slashes, as the second sed command wouldn't work.
root@dev:~# cat edit.sh
#!/bin/bash
for ((i=1; i<=100; i++))
do
newword=$(sed -n "${i}p" /root/list.txt)
sed "s/word1/$newword/g" /etc/dir$i/file.txt
done
root@dev:~# sh edit.sh
edit.sh: 2: edit.sh: Syntax error: Bad for loop variable
root@dev:~#
Perhaps your sh command is not bash. Try bash edit.sh instead.
The links in my signature might be of help.
Last edited by berndbausch; 01-15-2016 at 05:19 PM.
Thanks guys. To clarify, I am an entry level Linux technician so I don't know too much about mass editing files on an advanced scale like this, I just started working for a datacenter a few months ago and I am learning new things every day as I mainly work with Linux servers. I am working hard to become a systems administrator.
In my free time, I'm playing with development and bash scripting on my cloud servers. I'm eager to learn - and look forward to sharing my knowledge as I learn with the LinuxQuestions forums.
^ Doing the above, and also with "bash" seems to have run the command successfully.
However, upon actually visiting the files, it appears the word "word1" is still there and wasn't replaced with anything.
Is the sed command written correctly to search for "word1" and replace it with a line from /root/list.txt?
As I mentioned in my previous post, it will not change anything in the files. If you're happy with the output to the command line, you can add the -i flag to sed to make permanent changes to files:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.