shell sed

shell sed

Post by a » Sat, 25 Aug 2001 00:16:22



Hi, I am newbie in unix shell programming.
Can someone tell how to check that a variable that have a certain text?

    e.g.
    M=File.ddv
    if [ ${M} = "*.ddv" ]; then
        echo This is ddv file
    fi

   Obviously above does not work.
   How can do it with sed...?? [A-Za-z0-9]*.ddv ??

Thanks, AT

 
 
 

shell sed

Post by Rob Berci » Sat, 25 Aug 2001 00:54:53


There is probably a number of different tools and ways to accomplish this task
in a simpler manner, but if you want to do it with sed specifically do this.
for a good tutorial on sed to explain what I'm doing...goto:
http://www-106.ibm.com/developerworks/linux/library/l-sed2.html?dwzon...

M=File.ddv
FILE_EXTENSION=$(echo $M | sed -e s/^.*[.]//g )

if [ ${FILE_EXTENSION} = "ddv" ]; then
echo This is ddv file
fi

Quote:

>Hi, I am newbie in unix shell programming.
>Can someone tell how to check that a variable that have a certain text?

>    e.g.
>    M=File.ddv
>    if [ ${M} = "*.ddv" ]; then
>        echo This is ddv file
>    fi

>   Obviously above does not work.
>   How can do it with sed...?? [A-Za-z0-9]*.ddv ??

>Thanks, AT


 
 
 

shell sed

Post by Matthew Land » Sat, 25 Aug 2001 00:28:17



> Hi, I am newbie in unix shell programming.
> Can someone tell how to check that a variable that have a certain text?

>     e.g.
>     M=File.ddv
>     if [ ${M} = "*.ddv" ]; then
>         echo This is ddv file
>     fi

>    Obviously above does not work.
>    How can do it with sed...?? [A-Za-z0-9]*.ddv ??

> Thanks, AT

Use 2 [[]] instead of 1 [].  Otherwise you are getting filename
expansion out of the *.  And then take the * outside of the
""'s so it isn't considered a literal "*".

    M=File.ddv
    if [[ ${M} = *".ddv" ]]; then
        echo This is ddv file
    fi

 - Matt
--
_______________________________________________________________________

  IBM High Speed Interconnect - Fibre Channel I/O Dev/Test/Support
   << Comments, views, and opinions are mine alone, not IBM's. >>

 
 
 

shell sed

Post by Ben.Altma » Sat, 25 Aug 2001 01:09:56



> Hi, I am newbie in unix shell programming.
> Can someone tell how to check that a variable that have a certain text?

>     e.g.
>     M=File.ddv
>     if [ ${M} = "*.ddv" ]; then
>         echo This is ddv file
>     fi

Try (in Posix type shells):
    if [[ $M = *.ddv ]]; then

Quote:>    Obviously above does not work.
>    How can do it with sed...?? [A-Za-z0-9]*.ddv ??

You could use sed, but expr would be better:
    if [ `expr $M : ".*\.ddv` = 0 ]; then
        echo This is ddv file
    fi

(You could use -eq instead of = to emphasise you are dealing with numbers
but the above should work)
regards,
Ben

 
 
 

shell sed

Post by Ben.Altma » Sat, 25 Aug 2001 01:23:14




> > Hi, I am newbie in unix shell programming.
> > Can someone tell how to check that a variable that have a certain text?

> >     e.g.
> >     M=File.ddv
> >     if [ ${M} = "*.ddv" ]; then
> >         echo This is ddv file
> >     fi

> Try (in Posix type shells):
>     if [[ $M = *.ddv ]]; then

> >    Obviously above does not work.
> >    How can do it with sed...?? [A-Za-z0-9]*.ddv ??

> You could use sed, but expr would be better:
>     if [ `expr $M : ".*\.ddv` = 0 ]; then
>         echo This is ddv file

I slipped up - the above should be:
    if [ `expr $M : ".*\.ddv"` != 0 ]; then
        echo This is a ddv file
    fi
 
 
 

shell sed

Post by laura fairhe » Fri, 24 Aug 2001 18:09:17



Quote:>Hi, I am newbie in unix shell programming.
>Can someone tell how to check that a variable that have a certain text?

>    e.g.
>    M=File.ddv
>    if [ ${M} = "*.ddv" ]; then
>        echo This is ddv file
>    fi

>   Obviously above does not work.
>   How can do it with sed...?? [A-Za-z0-9]*.ddv ??

This is definently not a job for 'sed' (however
see below), use the shell built-in 'case';

case "$M" in
*.ddv) echo this is a ddv file;;
esac

There is documentation for 'case' on the shell
man-page (man sh).

If you really wanted to use 'sed' to match these strings
you would have to put a backslash before the period
(because otherwise a period will match any character)
also you would have to anchor it with ^ and $ to make
sure it matched only the whole string;

/^[A-Za-z0-9]*\.ddv$/

But making 'sed' somehow to tell the shell to act
one way or another because it matched the string
would just get plain ugly...

print the string if it matches (like grep);

echo $M |sed '/^[A-Za-z0-9]*\.ddv$/!d'

count the number of lines;

echo $M |sed '/^[A-Za-z0-9]*\.ddv$/!d' |wc -l

if the number of lines is 1 then its okay;

if [ `echo $M |sed '/^[A-Za-z0-9]*\.ddv$/!d' |wc -l` -eq 1 ]
then
  echo its a ddv file
fi

see what I mean !? ;) it could be done by instead getting
'sed' to print 'true' or 'false' and using that as a command
(and maybe could be made a bit shorter) but its not really
the tool for the job when you have 'case' (at least here,
where you only really need a shell pattern and not a regular
expression)

'grep' is (slightly) better here because it returns 'true' if
it finds a match and 'false' if it doesn't so you can
do away with more than half of the above but have to
add a >/dev/null to stop it printing stuff out;

if echo "$M" |grep '^[A-Za-z0-9]*\.ddv$' >/dev/null
then
  echo its a ddv file
fi

byefrom,
: ${L} # http://lf.8k.com:80

Quote:

>Thanks, AT

 
 
 

shell sed

Post by Juegen Hec » Sat, 25 Aug 2001 03:12:20


at schrieb:

Quote:> Hi, I am newbie in unix shell programming.
> Can someone tell how to check that a variable that have a certain text?

>     e.g.
>     M=File.ddv
>     if [ ${M} = "*.ddv" ]; then
>         echo This is ddv file
>     fi

>    Obviously above does not work.
>    How can do it with sed...?? [A-Za-z0-9]*.ddv ??

> Thanks, AT

In Posix- and Korn-Shell use the new  [[ ... ]]  (test-command)

[[  ${M} =  *.ddv  ]]   &&   echo This is ddv file

  btd.heck.vcf
< 1K Download
 
 
 

shell sed

Post by Bill Marcu » Sat, 25 Aug 2001 16:54:29



>Hi, I am newbie in unix shell programming.
>Can someone tell how to check that a variable that have a certain text?

>    e.g.
>    M=File.ddv
>    if [ ${M} = "*.ddv" ]; then
>        echo This is ddv file
>    fi

>   Obviously above does not work.
>   How can do it with sed...?? [A-Za-z0-9]*.ddv ??

case $M in
   *.ddv) echo This is ddv file ;;
   *) echo This is not ddv ;;
esac
 
 
 

shell sed

Post by George Athanassopoulo » Sat, 25 Aug 2001 18:46:46


Quote:> >    e.g.
> >    M=File.ddv
> >    if [ ${M} = "*.ddv" ]; then
> >        echo This is ddv file
> >    fi

  Maybe this would do what you lookin for?

  M="File.ddv"
  if [ "${M%.ddv}" != "${M}" ]
  then
   echo "This is ddv file"
  fi

--
=====================================================================
                       George Athanassopoulos                          
=====================================================================

 WWW   : http://www.egnatia.ee.auth.gr  http://www.real.macedonia.gr
=====================================================================
 PGP Key: http://egnatia.ee.auth.gr/~athanas/pgp_public_key.asc  
=====================================================================

 
 
 

shell sed

Post by a » Sat, 25 Aug 2001 22:52:22


Hi,
    I try your suggestion for a size.
    It didn't work.
    But FILE_EXTENSION=echo $M | sed -e 's/^.*[.]//g'
    is ok.

    I have not use $(...) b4. What is about?
    /andrew


> There is probably a number of different tools and ways to accomplish this task
> in a simpler manner, but if you want to do it with sed specifically do this.
> for a good tutorial on sed to explain what I'm doing...goto:
> http://www-106.ibm.com/developerworks/linux/library/l-sed2.html?dwzon...

> M=File.ddv
> FILE_EXTENSION=$(echo $M | sed -e s/^.*[.]//g )

> if [ ${FILE_EXTENSION} = "ddv" ]; then
> echo This is ddv file
> fi

> >Hi, I am newbie in unix shell programming.
> >Can someone tell how to check that a variable that have a certain text?

> >    e.g.
> >    M=File.ddv
> >    if [ ${M} = "*.ddv" ]; then
> >        echo This is ddv file
> >    fi

> >   Obviously above does not work.
> >   How can do it with sed...?? [A-Za-z0-9]*.ddv ??

> >Thanks, AT

 
 
 

shell sed

Post by a » Sat, 25 Aug 2001 23:06:14


Sorry, I try this it doesn't work.
    Error message bad subsitution.

    /andrew


> > >    e.g.
> > >    M=File.ddv
> > >    if [ ${M} = "*.ddv" ]; then
> > >        echo This is ddv file
> > >    fi

>   Maybe this would do what you lookin for?

>   M="File.ddv"
>   if [ "${M%.ddv}" != "${M}" ]
>   then
>    echo "This is ddv file"
>   fi

> --
> =====================================================================
>                        George Athanassopoulos                          
> =====================================================================

>  WWW   : http://www.egnatia.ee.auth.gr  http://www.real.macedonia.gr
> =====================================================================
>  PGP Key: http://egnatia.ee.auth.gr/~athanas/pgp_public_key.asc  
> =====================================================================

 
 
 

shell sed

Post by Beverly K. Pop » Mon, 27 Aug 2001 20:22:59


For a possibly shorter comparison, just look at the extension:

M="/users/myhome/mydir/work/file.ddv"
if [ "${M##*.}" = "ddv" ] ; then
    echo "This is a ddv file"
fi

"##*." removes the longest string from the front of the variable's contents
that ends
with a dot.  In this case, it removes "/users/myhome/mydir/work/file.",
leaving
"ddv".


> > >    e.g.
> > >    M=File.ddv
> > >    if [ ${M} = "*.ddv" ]; then
> > >        echo This is ddv file
> > >    fi

>   Maybe this would do what you lookin for?

>   M="File.ddv"
>   if [ "${M%.ddv}" != "${M}" ]
>   then
>    echo "This is ddv file"
>   fi

> --
> =====================================================================
>                        George Athanassopoulos
> =====================================================================

>  WWW   : http://www.egnatia.ee.auth.gr  http://www.real.macedonia.gr
> =====================================================================
>  PGP Key: http://egnatia.ee.auth.gr/~athanas/pgp_public_key.asc
> =====================================================================

 
 
 

shell sed

Post by George Athanassopoulo » Wed, 29 Aug 2001 18:41:48


Pope,
that is not quite correct because if the file is called ddv then the case
is screwed. If you M="ddv" then your script works incorrectly reporting
that "ddv" is a ddv file (meaning it has got a .ddv extension).
While:
#!/bin/bash
 M="ddv"
   if [ "${M%.ddv}" != "${M}" ]
   then
     echo "This is ddv file"
   fi

works just fine.


> For a possibly shorter comparison, just look at the extension:

> M="/users/myhome/mydir/work/file.ddv"
> if [ "${M##*.}" = "ddv" ] ; then
>     echo "This is a ddv file"
> fi

> "##*." removes the longest string from the front of the variable's contents
> that ends
> with a dot.  In this case, it removes "/users/myhome/mydir/work/file.",
> leaving
> "ddv".



> > > >    e.g.
> > > >    M=File.ddv
> > > >    if [ ${M} = "*.ddv" ]; then
> > > >        echo This is ddv file
> > > >    fi

> >   Maybe this would do what you lookin for?

> >   M="File.ddv"
> >   if [ "${M%.ddv}" != "${M}" ]
> >   then
> >    echo "This is ddv file"
> >   fi

> > --
> > =====================================================================
> >                        George Athanassopoulos
> > =====================================================================

> >  WWW   : http://www.egnatia.ee.auth.gr  http://www.real.macedonia.gr
> > =====================================================================
> >  PGP Key: http://egnatia.ee.auth.gr/~athanas/pgp_public_key.asc
> > =====================================================================

--
=====================================================================
                       George Athanassopoulos                          
=====================================================================

 WWW   : http://www.egnatia.ee.auth.gr  http://www.real.macedonia.gr
=====================================================================
 PGP Key: http://egnatia.ee.auth.gr/~athanas/pgp_public_key.asc  
=====================================================================
 
 
 

1. shell - sed - awk question

I have a couple of files from tape which were supposed to be 110 character,
fixed length records.  For some reason, the entire 16+ Mb files are apparently each a single line (no CR or LF or combination of them anywhere).  I cannot edit them using vi (line too long message occurs).

I can't find any line terminator characters in the files using grep.

I tried to figure a sed command to count 110 characters and output them, but failed.  Is this possible w/ sed?  Is it possible at all?

Short example for 10 char record length:

file looks like this:
1234.1234.987346 304 8 -38.24.902183 98748naskdj<EOF>

but should really look like this:
1234.1234.
987346 304
 8 -38.24.
902183 987
48naskdj  
<EOF>

Any help would be appreciated.

thanks,
-chuck

--
ACCEL Services, Inc.| Specialists in Gravity, Magnetics |  1(713)993-0671 ph.
1980 Post Oak Blvd. |   and Integrated Interpretation   |  1(713)960-1157 fax
    Suite 2050      |                                   |

                    |  President & Senior Geoscientist  |

     "Integration means more than having all the maps at the same scale!"

--
ACCEL Services, Inc.| Specialists in Gravity, Magnetics |  1(713)993-0671 ph.
1980 Post Oak Blvd. |   and Integrated Interpretation   |  1(713)960-1157 fax
    Suite 2050      |                                   |

                    |  President & Senior Geoscientist  |

     "Integration means more than having all the maps at the same scale!"

2. X and Private Networks

3. Shell: sed - renaming multiple files

4. PTBL

5. Basic shell/sed/awk question - float truncation and comparison

6. Top 10 posters comp.unix.shell

7. Calculating age in shell/sed script

8. Printing with SOLARIS 7 on SPARCstation 10

9. sed problem: using sed in a shell script

10. sed sed sed

11. . 2 sed FAQs, sed exes, sed cetras; URLs

12. SED SED SED...

13. (sed 1q ; sed 2q) : no output from 2nd 'sed'