Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: how to remove last character of a string

  1. #11
    Join Date
    Sep 2006
    Beans
    2,914

    Re: how to remove last character of a string

    Quote Originally Posted by finer recliner View Post
    I have a file of many words that looks something like this:
    Code:
    word1
    word2
    word3
    etc
    each word is a different length, and they all seem to have a trailing space (or some sort of non-printable character). Whats the easiest way to strip the last character (the space) from each line?

    Code:
    awk '{sub(/ +$/,"",$0)}1' file > newfile

  2. #12
    Join Date
    Mar 2007
    Beans
    39

    Re: how to remove last character of a string

    you can strip any last character with just string[:-1]

  3. #13
    Join Date
    Oct 2005
    Beans
    4

    Re: how to remove last character of a string

    could you provide an example of how you'd use
    Code:
    string[:-1]
    I'm trying to remove the * from an executable filename when doing an ls -l

  4. #14
    Join Date
    Sep 2006
    Beans
    2,914

    Re: how to remove last character of a string

    Quote Originally Posted by TeckniX View Post
    could you provide an example of how you'd use
    Code:
    string[:-1]
    I'm trying to remove the * from an executable filename when doing an ls -l
    how does your file name look like ?

  5. #15
    Join Date
    Feb 2006
    Location
    Vancouver, BC, Canada
    Beans
    318

    Re: how to remove last character of a string

    Quote Originally Posted by TeckniX View Post
    could you provide an example of how you'd use
    Code:
    string[:-1]
    I'm trying to remove the * from an executable filename when doing an ls -l
    Maybe you don't need to remove the last char at all.
    It's possible that you're getting the trailing * on some files because your "ls" command has the "-F" option, which appends a filetype classifier character to the filename.

    This may be the case even if you are not explicitly providing the "-F" yourself, often ls gets aliased to something that has some useful default options.

    Try using "\ls -l" (ls with a leading backslash) to get an unaliased ls, and see if that solves your issue.

  6. #16
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    Re: how to remove last character of a string

    Quote Originally Posted by TeckniX View Post
    could you provide an example of how you'd use
    Code:
    string[:-1]
    I'm trying to remove the * from an executable filename when doing an ls -l
    You shouldn't be parsing the output of ls in the first place. ls is designed for human readers; it's impossible to reliably parse its output regardless of which options you pass to it because you can't always tell where filename boundries are. Remember, a filename can contain ANY whitespace character, including \n. Instead, use your language's facility for handling filenames. E.g.,
    Code:
    #Bash
    for i in *; do
      # do stuff
    done
    
    #Ruby
    Dir.open(some_dir) do |dir|
      dir.each do |file|
        next if file =~ /^[.]+$/
        # do stuff
      end
    end

  7. #17
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: how to remove last character of a string

    What language was this supposed to be in anyway?

  8. #18
    Join Date
    Apr 2006
    Beans
    1,030
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: how to remove last character of a string

    Indeed, tell us what language and we will be glad to help

    MIke

  9. #19
    Join Date
    Sep 2006
    Beans
    2,914

    Re: how to remove last character of a string

    he is doing a ls -l. I am certain that's not a shell command.

  10. #20
    Join Date
    Oct 2006
    Location
    toxygen@jabber.sk
    Beans
    9

    Re: how to remove last character of a string

    sed -ie 's/.$//' filename

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

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