Announcement

Collapse
No announcement yet.

Unix scripting

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unix scripting

    Does anyone know how...in either korn, c, or bash, to basically do the equivalent to a RIGHT$ in BASIC?

    I need to figure out how to accept an input somehow, then only take the last X characters of it, and store them. Any ideas?

  • #2
    with perl or php.

    on regular expression functions
    typically ^ marks beginning and $ end of string.
    with ".jpg$" you would seach string ending with .jpg.
    I'm not familiar with perl, but some php functions are like ones in perl.
    dedicated server obtained, users close to Finland welcome

    Comment


    • #3
      I'm sure awk is what you want for this - don't ask me how you do it though (never used awk).

      I've got the O'Reilly sed&awk book on my shelf, so I guess if you reeeeeeeally want to know I'll look it up for you...
      Blah blah blah nick blah blah confusion, blah blah blah blah frog.

      Comment


      • #4
        Hey - I missed C in that list at first.

        You say you want input - from where? User prompt, command line parameter, from a file, from an environment variable, ...

        Is the number of characters to keep variable? If so, where is it specified? (same list of possible sources as the input string)

        Where do you want the result saved? (file, environment, printed to stdout, etc.)

        I know perl can do this (though I don't know perl)
        Awk can do it - you need something like print substr($0, length-X, X) as the guts of the awk script
        It's trivial in C, other than all the stuff you need for checking parameters or reading environment variables.

        It's probably doable in a shell script as well

        Note that any language that you can take the left X characters can also probably give you the right X (reverse string, take first X characters, reverse the result)

        - Steve

        Comment


        • #5
          Erm, by "C" I think he meant the C-shell...
          Blah blah blah nick blah blah confusion, blah blah blah blah frog.

          Comment


          • #6
            Yeah, you're right.
            (I'm a bonehead )

            - Steve

            Comment


            • #7
              OK. From the command line, you can extract the rightmost X characters with the following:
              Code:
              awk '{print substr( $0, length($0)-(X-1), X ) }'
              You will need some variation on that theme depending on where the input comes from, and where it's going:
              Code:
              awk '{print substr( $0, length($0)-9, 10 ) }' listfile > rightside
              will take the rightmost 10 characters of every line in the file listfile, and output them into the file rightside (one output line per input line).
              Code:
               echo $PWD | awk '{print substr( $0, length($0)-9, 10 ) }'
              will print the rightmost 10 characters of the current directory.
              ...
              modify at will for the desired effect.

              - Steve

              Comment


              • #8
                I did indeed mean c shell, sorry I wasn't clearer.

                spadnos - That should do nicely. Thanks!

                Comment

                Working...
                X