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. |
Hi and welcome to LQ.
See if that would work for you. Code:
for ((i=1; i<=100; i++)) 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. |
Quote:
I'm getting the following error when trying your script: edit.sh: 1: edit.sh: Syntax error: Bad for loop variable |
Quote:
Can you add: Code:
#!/bin/bash |
Quote:
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++)) |
What system are you running it in?
|
Quote:
The links in my signature might be of help. |
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. |
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 |
^ 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? |
Quote:
sed -i ".......... |
Ok thanks sycamorex. It works perfectly!!
@berndbausch thanks to you as well I will check out the links in your sig. |
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. |