LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-26-2010, 03:30 AM   #1
pramod.srk
Member
 
Registered: Feb 2009
Posts: 47

Rep: Reputation: 15
Post How to replace line with another line in shell script


Dear All,
I have the file abc.txt

cat abc.txt
This is a test file
Nothing is new in this world


I want to replace
"This is a test file" to "Text is replaced"

Code
Code:
FindString='This is a test file'
ReplaceString='Text is replaced'
Findarray=(`echo $FindString | tr ' ' ' '`)
ReplaceArray=(`echo $ReplaceString | tr ' ' ' '`)

# Length 
num_new_items=${#Findarray[@]};

# Length of replace array
ReplaceArrayLength=${#ReplaceArray[@]};

echo "Replace Array Length : $ReplaceArrayLength"

echo "Length $num_new_items";
for (( i=0;i<$ReplaceArrayLength;i++)); do
	sed  -i s/${Findarray[$i]}/${ReplaceArray[$i]}/g TestFile
#	echo ${array[$i]};
#	echo $i;
done

But this is not effective.
Any idea how to replace entire line either using sed or awk or any other utility.

Thanks in Advance,
Pramod
 
Old 02-26-2010, 03:35 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

how about:
Code:
sed 's/This is a test file/Text is replaced/' < abc.txt > abc.txt.new;  mv abc.txt.new abc.txt
Evo2.
 
Old 02-26-2010, 03:55 AM   #3
pramod.srk
Member
 
Registered: Feb 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks for the reply.
The changes are not reflected when i tried to run the following command.

export a="This is a test file"
export b="Text is replaced"
sed 's/$a/$b/' < abc.txt > abc.txt.new; mv -f abc.txt.new abc.txt
 
Old 02-26-2010, 04:17 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
The problem is that you have $a and $b between single quotes, meaning they will not expand to their variables.
Use double quotes if you want to expand shell variables. Eg
Code:
a="This is a test file"
b="Text is replaced"
sed "s/$a/$b/"  < abc.txt > abc.txt.new; mv -f abc.txt.new abc.txt
Evo2.

Last edited by evo2; 02-26-2010 at 04:18 AM. Reason: typo
 
Old 02-26-2010, 04:57 AM   #5
pramod.srk
Member
 
Registered: Feb 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Thumbs up

It worked.. :-)

Thank you very much.
 
Old 02-26-2010, 06:00 AM   #6
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
You can also use the -i option in place replacement
Code:
bash-3.1$ cat abc.txt
This is a test file
Nothing is new in this world
bash-3.1$ a="This is a test file";b="Text is replaced";sed -i "s/$a/$b/" abc.txt
bash-3.1$ cat abc.txt
Text is replaced
Nothing is new in this world
 
1 members found this post helpful.
  


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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM

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

All times are GMT -5. The time now is 11:58 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