How to get the leftmost n characters in a string - Aix

This is a discussion on How to get the leftmost n characters in a string - Aix ; I know just enough about scripting to be dangerous. I'm trying to write a simple one that prints several files in a directory to different printers, based on the name of the file, for instance, if the file name is ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: How to get the leftmost n characters in a string

  1. How to get the leftmost n characters in a string

    I know just enough about scripting to be dangerous. I'm trying to
    write a simple one that prints several files in a directory to
    different printers, based on the name of the file, for instance, if
    the file name is 3EAreport.ps, I want to send it to printer NP3EA001.
    I've figured out how to do pretty much everything except how to grab
    the first 3 characters of the file name

    What I have so far is:

    PrintFile()
    {
    local fn=$1
    local unit=LEFT(fn,3)
    local printer="NP"$unit"001"
    lpr -P$printer fn
    }
    # Main
    for FileName in /usr/spool/reports/*.ps
    do
    PrintFile $FileName
    done

    Of course the "LEFT" function won't work, but that's what I want to
    accomplish. I think some use of cut, or possibly awk, would do it,
    but i don't know how to code it.

  2. Re: How to get the leftmost n characters in a string

    rodak wrote:
    > I know just enough about scripting to be dangerous. I'm trying to
    > write a simple one that prints several files in a directory to
    > different printers, based on the name of the file, for instance, if
    > the file name is 3EAreport.ps, I want to send it to printer NP3EA001.
    > I've figured out how to do pretty much everything except how to grab
    > the first 3 characters of the file name
    >
    > What I have so far is:
    >
    > PrintFile()
    > {
    > local fn=$1
    > local unit=LEFT(fn,3)
    > local printer="NP"$unit"001"
    > lpr -P$printer fn
    > }
    > # Main
    > for FileName in /usr/spool/reports/*.ps
    > do
    > PrintFile $FileName
    > done
    >
    > Of course the "LEFT" function won't work, but that's what I want to
    > accomplish. I think some use of cut, or possibly awk, would do it,
    > but i don't know how to code it.


    cut -c1-3 will give you the leftmost 3 characters
    comp.unix.shell might be your group

  3. Re: How to get the leftmost n characters in a string

    On May 20, 10:34 am, rodak wrote:
    > I know just enough about scripting to be dangerous. I'm trying to
    > write a simple one that prints several files in a directory to
    > different printers, based on the name of the file, for instance, if
    > the file name is 3EAreport.ps, I want to send it to printer NP3EA001.
    > I've figured out how to do pretty much everything except how to grab
    > the first 3 characters of the file name
    >
    > What I have so far is:
    >
    > PrintFile()
    > {
    > local fn=$1
    > local unit=LEFT(fn,3)
    > local printer="NP"$unit"001"
    > lpr -P$printer fn}
    >
    > # Main
    > for FileName in /usr/spool/reports/*.ps
    > do
    > PrintFile $FileName
    > done
    >
    > Of course the "LEFT" function won't work, but that's what I want to
    > accomplish. I think some use of cut, or possibly awk, would do it,
    > but i don't know how to code it.


    The leftmost are actually quite easy to get. You can do this with the
    "cut" command.

    Example:
    PREFIX=$(echo $FileName| cut -c1-3)

    The rightmost are a bit more challenging, and you would need to know
    the length of the string (wc -c) and do some calculations (expr), but
    cut will work for that too.

    SteveN

  4. Re: How to get the leftmost n characters in a string

    another option could be to set a variable to be 3 char wide and copy
    the contents.

    typeset -L3 left_afn
    left_afn="$( echo $fn )"


    other ways could include printf.

    With refernce to the right use

    typeset -R3 right_afn
    right_afn="$( echo $fn )"


+ Reply to Thread