Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using 'sed' to remove multiple leading # characters in a file

Status
Not open for further replies.

forrie

MIS
Mar 6, 2009
91
US
I have a series of scripts that make an edit to a crontab file, to enable/disable a job as required. What I'm running into is multiple hash # symbols being injected which I can't figure out how to remove.

Here is how I am doing it:

REMOVE #

[blue]
/opt/csw/bin/gsed -i '/^.*\/usr\/local\/scripts\/mirror-fix\.sh.*/ s/^#//' $TEMPFILE
[/blue]

REPLACE #

[blue]
/opt/csw/bin/gsed -i '/^.*\/usr\/local\/scripts\/mirror-fix\.sh.*/ s/^/#/' $TEMPFILE
[/blue]


This approach may not be optimal, perhaps I just need to grep for the line and delete it, replacing it with another line that has the uncommented/commented entry.

The script should be able to be run repeatedly and not give the result it does, with ## prepended lines.

Otherwise, I am trying to figure out how to tell 'sed' to remove ^#(more than one hash).* up to the next character which is a number (crontab).

Any suggestions, pointers would be really appreciated, this is driving me up a wall... but a good learning experience.
 
I have not used gsed, but this is what I would do with sed

sed 's/^##*//'

So I think you were missing the "#*" which reads "remove 0 or more additional "#" signs".

If you can have spaces between the "#" signs like "## ## #" try this instead:

sed 's/^#[# ]*//'

And if tabs are possible you will have to include that too in the [ ] brackets.

And what if somebody creates the cron job with spaces or tabs before the first "#" ?

Things just got more complicated eh?
 
Thanks for the feedback.

I'm still having trouble with it. I think a different approach is probably needed here. Perhaps it would be more efficient to simply detect the line, delete it completely, then replace it with the content I need (be that hashed or unhashed).

?

I think that would be the same regex only ending with a /d then followed by a substitution?


Thanks.
 
What I provided should have worked. What are you getting? Provide the line form the cron file that is giving you grief
 
I managed to get this working through just substituting the entire line:

/usr/bin/sed -e "s/^.*mirror-fix.*/$OFF/g" $CRONTAB > $TEMPFILE

where $OFF is the entire line commented out, then $ON for the opposite and it works.

Probably not really ideal, but...
 
For sake of completion to this thread, here's what I ultimately ended up doing.

In the master script, I created two variables, ON and OFF, one has a prepended hash #, the other does not.

During the sed scan, I grep for the script name line in the crontab file, then replace it with the appropriate value:

/usr/bin/sed -e "s/^.*mirror-fix.*/$ON/g" $CRONTAB > $TEMPFILE

This works reliably every single time and eliminates the insanity of trying to match for every condition :)

Thanks again for your help, advice, etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top