sed pattern matching

sed pattern matching

Post by Elizabeth A Man » Thu, 17 Jun 1999 04:00:00



I need to be able to match *any* number of 0's that are between ||

Example

|000|00|0000000|0|

I would *hopefully* want that to be when I am done

|||||

I have used

sed -e 's/|0*|/||' filename

but that only gets every other one (i.e. sed reads up to a | for a
pattern, but for the next pattern it starts with the next character.. in
my case a 0)

Any clues?
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Elizabeth Musi                  University of Pittsburgh
Graduate Student Assistant      140 William Pitt Union
Office of Student Activities    (412) 648-7830

 
 
 

sed pattern matching

Post by Warren Porte » Thu, 17 Jun 1999 04:00:00



Quote:

>I need to be able to match *any* number of 0's that are between ||

>Example

>|000|00|0000000|0|

>I would *hopefully* want that to be when I am done

>|||||

>I have used

>sed -e 's/|0*|/||' filename

>but that only gets every other one (i.e. sed reads up to a | for a
>pattern, but for the next pattern it starts with the next character.. in
>my case a 0)

Try    sed -e 's/|0*|/||/g' filename

The "g" says global so it will work for all occurrences of the regular
expression.

Warren Porter
Rainy days OR Mondays Always Get Me Down.

 
 
 

sed pattern matching

Post by Kurt J. Lanz » Thu, 17 Jun 1999 04:00:00



> I need to be able to match *any* number of 0's that are between ||

> Example

> |000|00|0000000|0|

> I would *hopefully* want that to be when I am done

> |||||

> I have used

> sed -e 's/|0*|/||' filename

> but that only gets every other one (i.e. sed reads up to a | for a
> pattern, but for the next pattern it starts with the next character.. in
> my case a 0)

> Any clues?

Easy way -- do it twice:

        sed 's/|0*|/||/g
                s/|0*|//g' filename

Quick and dirty wins the race.

 
 
 

sed pattern matching

Post by Elizabeth A Man » Thu, 17 Jun 1999 04:00:00




Quote:>> Example

>> |000|00|0000000|0|

>> I would *hopefully* want that to be when I am done

>> |||||

>> I have used

>> sed -e 's/|0*|/||' filename

>> but that only gets every other one (i.e. sed reads up to a | for a
>> pattern, but for the next pattern it starts with the next character.. in
>> my case a 0)

>> Any clues?

>Easy way -- do it twice:

>    sed 's/|0*|/||/g
>            s/|0*|//g' filename

>Quick and dirty wins the race.

This still doesn't work:

Input (foo) file looks like:
 (fyi, this is just an example input file, my real input
  file is much larger with many possible combinations)

I will have more than one pattern case per line

|0|00|000|
|00.|
|.00|
|500|
|00000|0|

when I run:

sed -e 's/|0*|/||/g' -e 's/|0*|/||/g' foo

I get:

||00||
|00.|
|.00|
|500|
||0|  

What I need to get is

||||
|00.|
|.00|
|500|
|||

i.e. only where the pattern is *any* number of zeros (and only
zeros) do I want to substitute.

 
 
 

sed pattern matching

Post by Kurt J. Lanz » Thu, 17 Jun 1999 04:00:00





> >> Example

> >> |000|00|0000000|0|

> >> I would *hopefully* want that to be when I am done

> >> |||||

> >> I have used

> >> sed -e 's/|0*|/||' filename

> >> but that only gets every other one (i.e. sed reads up to a | for a
> >> pattern, but for the next pattern it starts with the next character.. in
> >> my case a 0)

> >> Any clues?

> >Easy way -- do it twice:

> >       sed 's/|0*|/||/g
> >               s/|0*|//g' filename

> >Quick and dirty wins the race.

> This still doesn't work:

[ snip long explanation of why it didn't work ]

Sorry. My fault. "*" in a RE says "any number, including none", so
/|0*|/ matches ||. What you want is:

        sed 's/|00*|/||/g
                s/|00*|/||g' yourfile

 
 
 

sed pattern matching

Post by Charles Dem » Thu, 17 Jun 1999 04:00:00




Quote:

>I need to be able to match *any* number of 0's that are between ||

>Example

>|000|00|0000000|0|

>I would *hopefully* want that to be when I am done

>|||||

>I have used

>sed -e 's/|0*|/||' filename

>but that only gets every other one (i.e. sed reads up to a | for a
>pattern, but for the next pattern it starts with the next character.. in
>my case a 0)

>Any clues?

Do it twice:

sed -e 's/|00*|/||/g;s/|00*|/||/g' infile

for this infile:

Quote:>|000|00|0000000|0|

it produces:

Quote:>|||||

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

sed pattern matching

Post by bmar.. » Fri, 18 Jun 1999 04:00:00



   >I need to be able to match *any* number of 0's that are between ||
   >Example
   >|000|00|0000000|0|
   >I would *hopefully* want that to be when I am done
   >|||||
   >I have used
   >sed -e 's/|0*|/||' filename
   >but that only gets every other one (i.e. sed reads up to a | for a
   >pattern, but for the next pattern it starts with the next character.
   >. in my case a 0)
   >Any clues?
Does your input file contain anything other than | and 0?  If not you could
simply use sed 's/0*//g'

Net-Tamer V 1.08X - Test Drive

 
 
 

1. Sed pattern matching

Hi folks,

Long time ago I wrote to a newsgroup. Hope I don't forget something...

I have a ksh-script here with following sed command in it:

echo $entry | sed -e /^$user:/s/^$user:$pwd[^:]*:/$user:${pwd},B.nP:/

The problem is that in the var $pwd are / too. But when I replace the /
as seperator with something else that does not appear in the vars, I
always get a "Unrecognized command: "
OS is Solaris and it's the UNIX sed I use.
How can get this working?

Thanks for any help.

Cheers,

        Markus

2. newbie: static ip laptop behind dynamic ip ppp link

3. sed pattern matching problem

4. icon program execution

5. Sed pattern matching.

6. DHCP, SAMBA and printers

7. sed pattern matching (thanks)

8. x application locks desktop

9. Sed Pattern Matching

10. Pattern matching and extracting the data which matches the pattern

11. Tricky SED matching and replace pattern for reformatting source codes brackets

12. sed: output matched pattern?

13. print out just the pattern matched using sed (performance)