Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2004
    Posts
    70

    Unanswered: String formatting?

    I am given a string like this: "20040920"
    It represents 2004 as the year 09 as the month and 20 as the day.

    How can I cut that string down to 0409?

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    echo '20040920' | sed -e 's/^....//g'

    or [in ksh]:
    a='20040920'
    a="${a##????}"
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

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

    Wink

    Also:
    echo '20040920' | cut -c3-6

    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  4. #4
    Join Date
    Jan 2004
    Posts
    70
    what if I had jobxxx.ini and I wanted xxx? but I didnt know the length of xxx?

    so it could be job1234.ini or job123.ini?

    Thanks again

    This is how I force it to work:

    export JOBNUMBER=`echo $1 | sed -e 's@.inp@@'`
    export JOBNUMBER=`echo ${JOBNUMBER} | sed -e 's@job@@'`

    Im sure there is a better way though...
    Last edited by JamesAvery22; 11-17-04 at 15:24.

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

    Cool

    Try:
    Code:
    a=$(echo "job1234.ini"|cut -c5-)
    a="${a%.*}"

    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  6. #6
    Join Date
    Jan 2004
    Posts
    70
    Quote Originally Posted by LKBrwn_DBA
    Try:
    Code:
    a=$(echo "job1234.ini"|cut -c5-)
    a="${a%.*}"


    echo "job134.ini"|cut -c4-

    works for me, -c5- cuts the first digit off.

    That does work though, thanks

    I was hoping for a one liner though =\

Posting Permissions

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