Searching for regexp

Searching for regexp

Post by Davi » Thu, 11 Mar 2004 19:36:03



Dear all,

I'm trying to create a regular expression that takes a list of space
delimited words and numbers and encloses every non-number within single
quotes.

e.g 1967 foo 3.234 system_analyst 87834/d23434 would be returned as 1967
'foo' 3.234 'system_analyst' '87834/d23434'

I can't get the syntax right ... how could you do this with sed?

 
 
 

Searching for regexp

Post by Stephane CHAZELA » Thu, 11 Mar 2004 21:05:00


2004-03-10, 21:36(+11), David:

Quote:> I'm trying to create a regular expression that takes a list of space
> delimited words and numbers and encloses every non-number within single
> quotes.

> e.g 1967 foo 3.234 system_analyst 87834/d23434 would be returned as 1967
> 'foo' 3.234 'system_analyst' '87834/d23434'

Assuming you want only decimal numbers (not 0x12 or 1.2e-1)

sed 's/.*/\
& /;t1
:1
/\n$/{s/..$//;b

Quote:}

s/\(\n\)\([-+]\{0,1\}[0-9][0-9]* \)/\2\1/
s/\(\n\)\([-+]\{0,1\}[0-9]*\.[0-9][0-9]* \)/\2\1/
t1
s/\(\n\)\([^ ]*\) /'\''\2'\'' \1/
b1'

Or:

awk -F '[ ]' '{
   for (i=1;i<=NF;i++)
     if ($i !~ /^[-+]?([0-9]+|[0-9]*\.[0-9]+)/)
       $i = "'\''" $i "'\''"
   print}'

For:

A B 1.2 .4 -1  123

it gives:

'A' 'B' 1.2 .4 -1 '' 123

--
Stphane                      ["Stephane.Chazelas" at "free.fr"]

 
 
 

Searching for regexp

Post by blfs » Thu, 11 Mar 2004 21:17:10



> Dear all,

> I'm trying to create a regular expression that takes a list of space
> delimited words and numbers and encloses every non-number within single
> quotes.

> e.g 1967 foo 3.234 system_analyst 87834/d23434 would be returned as 1967
> 'foo' 3.234 'system_analyst' '87834/d23434'

> I can't get the syntax right ... how could you do this with sed?

It's not a very clean solution but it works:

bash-2.05a$ echo "1967 foo 3.234 system_analyst 87834/d23434" | sed 's/0/00/g' | sed 's/\./0/g' | sed 's/\([^ ]*[^0-9 ][^0-9 ]*[^ ]*\)/!\1\!/g' | sed 's/\([^0]\)\(0\)\([^0]\)/\1.\3/'
1967 !foo! 3.234 !system_analyst! !87834/d23434!

 
 
 

Searching for regexp

Post by Stephane CHAZELA » Thu, 11 Mar 2004 21:24:02


2004-03-10, 13:17(+01), blfs:
[...]

Quote:> It's not a very clean solution but it works:

> bash-2.05a$ echo "1967 foo 3.234 system_analyst 87834/d23434" | sed 's/0/00/g' | sed 's/\./0/g' | sed 's/\([^ ]*[^0-9 ][^0-9 ]*[^ ]*\)/!\1\!/g' | sed 's/\([^0]\)\(0\)\([^0]\)/\1.\3/'
> 1967 !foo! 3.234 !system_analyst! !87834/d23434!

$ echo "0.123" | sed 's/0/00/g' | sed 's/\./0/g' | sed 's/\([^ ]*[^0-9 ][^0-9 ]*[^ ]*\)/!\1\!/g' | sed 's/\([^0]\)\(0\)\([^0]\)/\1.\3/'
000123

--
Stphane                      ["Stephane.Chazelas" at "free.fr"]

 
 
 

Searching for regexp

Post by blfs » Thu, 11 Mar 2004 21:31:06



> 2004-03-10, 13:17(+01), blfs:
> [...]
> > It's not a very clean solution but it works:

> > bash-2.05a$ echo "1967 foo 3.234 system_analyst 87834/d23434" | sed 's/0/00/g' | sed 's/\./0/g' | sed 's/\([^ ]*[^0-9 ][^0-9 ]*[^ ]*\)/!\1\!/g' | sed 's/\([^0]\)\(0\)\([^0]\)/\1.\3/'
> > 1967 !foo! 3.234 !system_analyst! !87834/d23434!

> $ echo "0.123" | sed 's/0/00/g' | sed 's/\./0/g' | sed 's/\([^ ]*[^0-9 ][^0-9 ]*[^ ]*\)/!\1\!/g' | sed 's/\([^0]\)\(0\)\([^0]\)/\1.\3/'
> 000123

You've been faster than me! :-)
 
 
 

Searching for regexp

Post by blfs » Thu, 11 Mar 2004 21:57:00



> 2004-03-10, 13:17(+01), blfs:
> [...]
> > It's not a very clean solution but it works:

> > bash-2.05a$ echo "1967 foo 3.234 system_analyst 87834/d23434" | sed 's/0/00/g' | sed 's/\./0/g' | sed 's/\([^ ]*[^0-9 ][^0-9 ]*[^ ]*\)/!\1\!/g' | sed 's/\([^0]\)\(0\)\([^0]\)/\1.\3/'
> > 1967 !foo! 3.234 !system_analyst! !87834/d23434!

> $ echo "0.123" | sed 's/0/00/g' | sed 's/\./0/g' | sed 's/\([^ ]*[^0-9 ][^0-9 ]*[^ ]*\)/!\1\!/g' | sed 's/\([^0]\)\(0\)\([^0]\)/\1.\3/'
> 000123

this should be better:
bash-2.05a$ echo "1967 foo 0.04 system_analyst 87834/d23434" | sed 's/\([^ ]*[^0-9 ][^0-9 ]*[^ ]*\)/!\1\!/g'  | sed 's/!\([0-9][0-9]*\.[0-9][0-9]*\)!/\1/'
1967 !foo! 0.04 !system_analyst! !87834/d23434!
 
 
 

Searching for regexp

Post by Stephane CHAZELA » Thu, 11 Mar 2004 22:00:52


2004-03-10, 13:57(+01), blfs:
[...]

Quote:> this should be better:
> bash-2.05a$ echo "1967 foo 0.04 system_analyst 87834/d23434" | sed 's/\([^ ]*[^0-9 ][^0-9 ]*[^ ]*\)/!\1\!/g'  | sed 's/!\([0-9][0-9]*\.[0-9][0-9]*\)!/\1/'
> 1967 !foo! 0.04 !system_analyst! !87834/d23434!

Yes, with a "g" for the second substitution

sed -e 's/\([^ ]*[^0-9 ][^0-9 ]*[^ ]*\)/'\''\1'\''/g' \
    -e 's/'\''\([0-9][0-9]*\.[0-9][0-9]*\)'\''/\1/g'

--
Stphane                      ["Stephane.Chazelas" at "free.fr"]

 
 
 

Searching for regexp

Post by Bill Marcu » Thu, 11 Mar 2004 20:55:48


On Wed, 10 Mar 2004 21:36:03 +1100, David

> Dear all,

> I'm trying to create a regular expression that takes a list of space
> delimited words and numbers and encloses every non-number within single
> quotes.

> e.g 1967 foo 3.234 system_analyst 87834/d23434 would be returned as 1967
> 'foo' 3.234 'system_analyst' '87834/d23434'

> I can't get the syntax right ... how could you do this with sed?

I think it would be easier with awk:
awk 'BEGIN{sq="\047"}
     {for(x=1;x<=NF;x++){
       if ($x !~/-?[0-9.]+/) $x=sq $x sq
       }
     }'

--
Incrsease your earoning poswer and gaerner profwessional resspect.
Get the Un1iversity Dewgree you have already earned.
 [from the prestigious, non-accredited University of Spam!]

 
 
 

Searching for regexp

Post by Stefan Lagotzk » Fri, 12 Mar 2004 05:14:52



Quote:> I think it would be easier with awk:
> awk 'BEGIN{sq="\047"}
>      {for(x=1;x<=NF;x++){
>        if ($x !~/-?[0-9.]+/) $x=sq $x sq
>        }
>      }'

Yes, but your solution does not match the OP's last field
87834/d23434 and it does not print the result.

awk 'BEGIN {sq="\047"}
   {for(x=1;x<=NF;x++){
      if ($x !~/^(-?[0-9]+(\.[0-9]*)?|-?\.[0-9]+)$/) $x=sq $x sq;
   }
   print }' << EOF

The Example was: 1967 foo 3.234 system_analyst 87834/d23434
but it also works with -1967 and -.7853981633  :-)
EOF

'The' 'Example' 'was:' 1967 'foo' 3.234 'system_analyst' '87834/d23434'
'but' 'it' 'also' 'works' 'with' -1967 'and' -.7853981633 ':-)'

 
 
 

Searching for regexp

Post by rakesh shar » Sat, 13 Mar 2004 13:16:43



> I'm trying to create a regular expression that takes a list of space
> delimited words and numbers and encloses every non-number within single
> quotes.

> e.g 1967 foo 3.234 system_analyst 87834/d23434 would be returned as 1967
> 'foo' 3.234 'system_analyst' '87834/d23434'

> I can't get the syntax right ... how could you do this with sed?

Assuming numbers to be only integers and floating points, we can accomplish
the above as:

sed -e '
   s/.*/ & /
   s/\([ \t]\)\([^ \t]\)/\1\
\2/g
   s/\([^ \t]\)\([ \t]\)/\1\
\2/g
   s/\n\([-+]\{0,1\}\.\{0,1\}\)\n/'\''\1'\''/g
   s/\n\([-+]\{0,1\}[0-9]*\.\{0,1\}[0-9]*\)\n/\1/g
   s/\n\([^ \t]*\)\n/'\''\1'\''/g
' yourfile

Or, we can loop over the fields as:

sed -e '
    s/[^ \t]/\
&/;s/$/ /
    :loop
    s/ \n$//;t
    s/\(\n\)\([-+]\{0,1\}\.\{0,1\}[ \t]\{1,\}\)/'\''\2'\''\1/;tloop
    s/\(\n\)\([-+]\{0,1\}[0-9]*\.\{0,1\}[0-9]*[ \t]\{1,\}\)/\2\1/;tloop
    s/\(\n\)\([^ \t]\{1,\}[ \t]\{1,\}\)/'\''\2'\''\1/;bloop
' yourfile

 
 
 

1. Search & Delete Repetitions of Regexp

    I'd like to search a file for a  regexp,  then  delete all
occurences of that  regexp *below*  the  first incidence
of it,   doing the same,  line for line through the file....

The only way I've been able to do it is with a tangled
web of temporary files using sed and grep that is not even
worth posting....

Any ideas?

Thanks,

Bruce<+>

2. HELP!! resolving dead in 1.1.60

3. Searching a file for 2 different regexp on 2 adjacent lines

4. RamDisk Problem: System Hang During Linux Installation

5. searching regexp

6. Iomega SCSI Zip 100

7. Linux-search the web quickly with Search Spaniel

8. mirror write consistency

9. UNIX-Shell-search the web quickly with Search Spaniel

10. UNIX-Programming-search the web quickly with Search Spaniel

11. searching for indexing/search software

12. Search file from 'bottom to top', varying search ?

13. NEED SEARCH ENGINE / ROBOT: Search our site and all linked sites