LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-15-2016, 04:25 PM   #1
youbuntoo
LQ Newbie
 
Registered: Jan 2016
Posts: 9

Rep: Reputation: Disabled
Question 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.
 
Old 01-15-2016, 04:50 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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.

Last edited by sycamorex; 01-15-2016 at 04:53 PM.
 
1 members found this post helpful.
Old 01-15-2016, 05:07 PM   #3
youbuntoo
LQ Newbie
 
Registered: Jan 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sycamorex View Post
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
 
Old 01-15-2016, 05:10 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by youbuntoo View Post
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?
 
Old 01-15-2016, 05:13 PM   #5
youbuntoo
LQ Newbie
 
Registered: Jan 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sycamorex View Post
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:~#
 
Old 01-15-2016, 05:15 PM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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.
 
Old 01-15-2016, 05:16 PM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
What system are you running it in?
 
Old 01-15-2016, 05:18 PM   #8
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by youbuntoo View Post
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.

Last edited by berndbausch; 01-15-2016 at 05:19 PM.
 
Old 01-15-2016, 05:18 PM   #9
youbuntoo
LQ Newbie
 
Registered: Jan 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 01-15-2016, 05:25 PM   #10
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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
 
Old 01-15-2016, 05:27 PM   #11
youbuntoo
LQ Newbie
 
Registered: Jan 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
^ 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?
 
Old 01-15-2016, 05:31 PM   #12
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by youbuntoo View Post
^ 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 "..........
 
1 members found this post helpful.
Old 01-15-2016, 05:35 PM   #13
youbuntoo
LQ Newbie
 
Registered: Jan 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Ok thanks sycamorex. It works perfectly!!

@berndbausch thanks to you as well I will check out the links in your sig.
 
Old 01-15-2016, 05:36 PM   #14
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Glad it worked. You can mark the thread as solved in thread tools
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stripping all but lines matching string from text files d.vanheeckeren Linux - General 5 04-29-2013 08:00 AM
[SOLVED] Sed - Replacing only text with several specific lines excluded Potatos Linux - Newbie 6 06-17-2010 11:51 PM
Replacing text in files without using sed dfresh4130 Programming 16 05-28-2009 10:13 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration