LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Mass Editing files with 'sed', replacing a string of text with different lines (https://www.linuxquestions.org/questions/linux-newbie-8/mass-editing-files-with-sed-replacing-a-string-of-text-with-different-lines-4175564060/)

youbuntoo 01-15-2016 04:25 PM

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.

sycamorex 01-15-2016 04:50 PM

Hi and welcome to LQ.

See if that would work for you.


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

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.

youbuntoo 01-15-2016 05:07 PM

Quote:

Originally Posted by sycamorex (Post 5478904)
Hi and welcome to LQ.

See if that would work for you.


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

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

sycamorex 01-15-2016 05:10 PM

Quote:

Originally Posted by youbuntoo (Post 5478914)
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


Can you add:
Code:

#!/bin/bash
to the beginning of the file?

youbuntoo 01-15-2016 05:13 PM

Quote:

Originally Posted by sycamorex (Post 5478917)
Can you add:
Code:

#!/bin/bash
to the beginning of the file?

Done:

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:~#

berndbausch 01-15-2016 05:15 PM

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.

sycamorex 01-15-2016 05:16 PM

What system are you running it in?

berndbausch 01-15-2016 05:18 PM

Quote:

Originally Posted by youbuntoo (Post 5478922)
Done:

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.

youbuntoo 01-15-2016 05:18 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.

--

I am running this on Ubuntu 15

Edit: will try "bash" and report back.

sycamorex 01-15-2016 05:25 PM

Try running it as (just make sure that it has exec permissions (chmod +x edit.sh)):

Code:

./edit.sh

as opposed to specifying the shell by:
Code:

sh edit.sh

youbuntoo 01-15-2016 05:27 PM

^ 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?

sycamorex 01-15-2016 05:31 PM

Quote:

Originally Posted by youbuntoo (Post 5478940)
^ 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:

sed -i "..........

youbuntoo 01-15-2016 05:35 PM

Ok thanks sycamorex. It works perfectly!!

@berndbausch thanks to you as well I will check out the links in your sig.

sycamorex 01-15-2016 05:36 PM

Glad it worked. You can mark the thread as solved in thread tools


All times are GMT -5. The time now is 09:25 AM.