LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-28-2010, 09:55 PM   #1
forrie
Member
 
Registered: Sep 2003
Distribution: RedHat
Posts: 41

Rep: Reputation: 15
Using SED to strip multiple leading hash symbols from a file


I have a quirky situation whereby I'm using SED to selectively comment out a line in a crontab job (on Solaris, I know but it's connected to the Linux function I'm working with).

What's happening is this.

Remove hash symbol
Code:
/opt/csw/bin/gsed -i '/^.*\/usr\/local\/scripts\/mirror-fix\.sh.*/ s/^#//' $TEMPFILE
Restore hash symbol
Code:
/opt/csw/bin/gsed -i '/^.*\/usr\/local\/scripts\/mirror-fix\.sh.*/ s/^/#/' $TEMPFILE
The problem I'm running into is that the script can sometimes prepend an extra hash # symbol if run more than once (I have a lockfile that I poll for to discourage this, but that's not perfect).

I wonder how I can modify that sed statement to remove any/all leading hash marks up to the first other character, in this case it's a 0 (zero) which is a crontab file.

Thanks!
 
Old 10-28-2010, 10:04 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Try this replacing your substitute command with this:
Code:
s/^#*//
 
Old 10-29-2010, 01:05 AM   #3
forrie
Member
 
Registered: Sep 2003
Distribution: RedHat
Posts: 41

Original Poster
Rep: Reputation: 15
No luck, I still ended up with repeat hash symbols on the reverse and they were not corrected with the asterisk.

Thanks, tho.

Quote:
Originally Posted by crts View Post
Try this replacing your substitute command with this:
Code:
s/^#*//
 
Old 10-29-2010, 02:56 AM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
s/^#0/0/g

or maybe

s/^##/#/g

To turn ## into #
 
Old 10-29-2010, 01:35 PM   #5
forrie
Member
 
Registered: Sep 2003
Distribution: RedHat
Posts: 41

Original Poster
Rep: Reputation: 15
The first will work (I tried it); however, because of the way the script runs, a repeat run will add another #.

The second option will work if you assume that there's no other problem and only 2 hashes exist.

I think a better solution might be (which I'm figuring out) to tell 'sed' to replace 1 or more instances of a # that begins at ^ up to the next alphanumeric character, which in this case is always 0 (zero).

OR

Use 'sed' to simply search for the matching line, delete it and replace with another full line that has the hash removed already. This might be more efficient/reliable.

Suggestions welcomed. This is one of those good learning experiences... :-)

Quote:
Originally Posted by dive View Post
s/^#0/0/g

or maybe

s/^##/#/g

To turn ## into #
 
  


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
[SOLVED] What is the use of combination of symbols tilde and hash viz. ~# ?? Mr. ameya sathe Red Hat 4 03-17-2011 12:30 AM
strip of blanks using sed. visitnag Linux - Desktop 1 02-13-2009 08:26 AM
sed newbie question - strip leading character only if 0 Frayed Programming 6 05-22-2008 10:30 AM
Strip leading dot in filepaths (in tar archive) EcceVery Debian 2 04-13-2005 07:47 PM
How sed strip /* comments aaronzh Programming 1 06-05-2003 05:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:58 PM.

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