Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2009
    Posts
    56

    Unanswered: Extract a portion of string from file name !!

    Hello ,

    I have a file like 201404_xx_xxx_xx.dat.pgp

    I want to extract only upto .dat ie 201404_xx_xxx_xx.dat

    Pls advice !!

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    In sh/ksh
    Code:
    $filename=201404_xx_xxx_xx.dat.pgp
    shortname=`echo $filename |cut -c1-18`
    in bash
    Code:
    $filename=2014_xx_xxx_xx.dat.pgp
    shortname=${filename:0:18}

  3. #3
    Join Date
    Nov 2009
    Posts
    56
    I have a another file with pattern 201404_xx_xxx.dat.pgp
    , I can't use the same command as you stated . I am looking for single command that will take care of any file format ..it should just extract the complete string upto .dat

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    filename="my dog has fleas.dat.pgp"
    shortname=`echo $filename|cut -f1 -d"."`
    echo $shortname
    echo $shortname.dat

  5. #5
    Join Date
    Nov 2009
    Posts
    56
    Thanks Issue resolved .

    Thanks once again for your prompt and timely response . I really appreciate your time .

  6. #6
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    Quote Originally Posted by MIKELALA View Post
    Thanks Issue resolved .
    . . .
    You sure?
    Try this:
    Code:
    ==> filename=201404_xx_xxx_xx.dat.pgp
    ==> echo ${filename%%.*}
    201404_xx_xxx_xx
    
    ==> filename=2014_xx_xxx_xx.dat.pgp
    ==> echo ${filename%%.*}
    2014_xx_xxx_xx
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •