LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2012, 06:28 AM   #1
TheOnlyQ
Member
 
Registered: Apr 2011
Posts: 33

Rep: Reputation: 0
Sed replace not working in bash script.


Hi,

My target file is example.conf (which is actually $5, a variable in bash)

Code:
ConfigLine1 Option1 Option2
ConfigLine2 Option1 Option2
ConfigLine3 Option1 Option2 Option3
So I'm running this..

sed "1 c\ConfigLine1 $3 Option2=$1\\" /etc/3732/$5

$3 & $5 being variables defined when executing the command (./bash.sh Variable1 Variable2 Variable3 Variable4 Variable5)

My main issue, is that this one command will work perfectly fine, but I need to run 3 different sed commands (replace 3 different lines) with this bash script.

I've tried 3 different lines/commands, I've tried one line with the -e option, I've tried putting each command into an entirely new bash script and executing them manually..

But no matter what I do, the end result is an unchanged file - Although with one of the methods above the file is effected by the last sed, but only the last sed. Its as if each sed command edits the ORIGINAL and overwrites changes from the previous sed command, which makes NO sense to me, because I've also tried to split the commands into separate files which would fix that type of issue.

I've sat here for hours trying EVERYTHING. I've searched for a solution as much as possible, and after reading countless LQ threads I decided to create my own.

Just so everyone knows, I'm going to be passing these variables via ssh2_connect with PHP though this shouldn't make any real difference.

Please provide me with a solution, things like this really cause a halt in my productivity.
 
Old 06-20-2012, 06:56 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
confused I am..........

by default, sed will not write to a file . You have 2 options:

Use the -i flag, telling sed to edit the file in place

Redirect the sed output to a newfile

(I think it is bad practice to try and write to the same file you are reading from, so either use the -i flag, or write to a new file (which you can then rename back to the old name) ).
 
Old 06-21-2012, 12:33 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As mentioned, sed should work just fine, with the -i option. I recommend using multiple -e expressions so you only need to execute one command.

As another option, you might consider using ed instead.

Code:
ed -s "/etc/3732/$5file" <<HEREDOC
1c
ConfigLine1 $3 Option2=$1
.
2c
ConfigLine2 foo bar
.
3c
ConfigLine3 baz bum
w
HEREDOC
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell Script - Cant get sed to replace a line from a textfile CrowdstarQA Programming 4 02-23-2012 11:53 AM
[SOLVED] BASH :sed find and replace nano2 Linux - General 2 10-24-2011 04:56 PM
Search and Replace in bash script daemoneye Other *NIX 1 04-08-2009 01:24 AM
How to replace space using sed in shell script Ashok_mittal Linux - Newbie 2 09-08-2008 07:05 AM
bash or perl replace .. maybe sed hostpc Linux - Software 4 10-11-2007 04:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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