sed to delete character 0 only when it's on its own?

Hi all

I am trying to get my head around doing the following....

I have an input field that could contain either a number a blank field or a whitespace field.

What I want to do is delete a 0 (zero) if it's on its own or leading the number.

So:-

\t0 delete the zero
0 delete the zero
0\t delete the zero

10 do NOT delete the zero
\t10 do NOT delete the zero

Please do not just post a response showing a solution without explaining how it works else I can't learn for next time.

Thanks in advance.

---------- Post updated at 12:05 PM ---------- Previous update was at 12:03 PM ----------

Sorry I should add I want to do this using just sed for reasons too boring to explain here.

But we are interested, it could be homework....:rolleyes:

It's actually an input field from a SQL script pulling the field from a mythTV database. The input field is identifying the episode number of total episodes. However as the input field is being sent as part of an EIT packet by the originating broadcaster you can't guarantee the format, thus the need to delete either 0 or whitespace etc.....

It's not homework, it's a script I'm working on as a process of trying to learn sed.

OK?

Try this:

sed 's/^\([ 	]*\)0\(.*\)/\1\2/' file

Within the square blocks there are a space and a tab. Type the tab character as <Ctrl>V <TAB>.

# cat infile
 
this is tab and zero    0 delete this zero
this is one zero 0 delete this zero
this is zero and tab 0  delete this zero
 
10 no delete this
        10 no delete this
 
# sed 's/\(.*\)\b0\(.*\)/\1\2/' infile
 
this is tab and zero     delete this zero
this is one zero  delete this zero
this is zero and tab    delete this zero
 
10 no delete this
        10 no delete this

Try this -

sed -e 's/[ ]\{1,\}0/ /g' file

Within the square blocks there are a space and a tab. Type the tab character as <Ctrl>V <TAB>.

Here's another one -

$
$
$ cat f1
this is tab and zero    0 delete this zero
this is one zero 0 delete this zero
this is zero and tab 0  delete this zero
0 this line has a leading zero
10 no delete this
        10 no delete this
this line has a trailing zero 0
$
$
$ sed 's/\b0\b//g' f1
this is tab and zero     delete this zero
this is one zero  delete this zero
this is zero and tab   delete this zero
 this line has a leading zero
10 no delete this
        10 no delete this
this line has a trailing zero
$
$

tyler_durden

Thanks Guys

Franklin I used your example, can you please explain to me how yours is working?

The reason I ask is I really need to make sure any leading character (not just a tab) is catered for, it could be something stupid like a newline or a return or a space. I assume I need to add any 'possible' characters in the first square box field? TV companies are not renowned for their attention to detail when it comes to populating these data fields.

Regards

Here we go:

sed 's/^\([ 	]*\)0\(.*\)/\1\2/' file

Explanation:

^\([ 	]*\)	# assign any existing spaces and tabs at the beginning of the line to the first remembered pattern (portion)
0		# there must be a 0 between the 1st and the next portion
\(.*\)		# assign the remaining string after the 0 to the second portion

\1		# print the 1st portion
\2		# print the 2st portion

Have a read of a tutorial:

Sed - An Introduction and Tutorial

Regards