Solved

SED Help

Posted on 2012-03-23
6
441 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Utilizing an array to gracefully append to a list of EmailAddresses
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

777 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