Solved

SED Help

Posted on 2012-03-23
6
444 Views
Last Modified: 2013-11-13
Hi all,

I am currently writing a command that allows me to appending a '#' in line of a text to help me disable a script (main.sh) from calling a sub-script (test.sh)

I have managed to get it working like this:

sed -i 's:./test.sh:#./test.sh:g' main.sh

Open in new window


however i need to have another script to have the '#' removed to re-enable the call.

and it works like this:

sed -i 's:#./test.sh:./test.sh:g' main.sh

Open in new window


it works ok assuming that the first command is only run once, however should it be run multiple times (in this case 4 times), this is the output:

####test.sh

as a result the second script needs to be run 4 times in to remove all instances of '#'.

How should i approach this?

 Should i fix the first script to ensure only one # is allowed to be appended prior to test.sh? or
should i fix the second script to ensure all instances of '#' are removed before test.sh?

appreciate any help as i am still relatively new to SED.

Thanks!
0
Comment
Question by:jaglin84
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 134 total points
ID: 37756210
sed -i 's:#*./test.sh:./test.sh:g' main.sh
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 134 total points
ID: 37756217
or
sed -i 's:#*./test.sh:#./test.sh:g' main.sh
0
 

Expert Comment

by:TheDoug
ID: 37783570
Sed uses regular expressions in its syntax, so you only need to add a * after a char to consider it multiple times
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 66 total points
ID: 37828318
Alternatively, fix the problem in the "comment out" script - use this (assuming that the "." of "./test.sh" is in the first column of the file):
sed -i 's:^./test.sh:#./test.sh:g' main.sh

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 38249566
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question