Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2004
    Location
    India
    Posts
    1

    Unanswered: Cut the Last Field

    How to Cut the Last Field in an output i received.

    Ex : $ ls -lt ./module/src/prog-name.pc
    ./module/src/prog-name.pc

    I need to display only the last field only ie. prog-name.pc

    how to do that.

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    If you want to display the last field only :
    Code:
    ls -lt ./module/src/ | awk '{print $NF}'
    To display the file name only from a full path :
    Code:
    basename ./module/src/prog-name.pc
    You can combine the two :
    Code:
    basename `ls -lt ./module/src/ | awk '{print $NF}'`
    Jean-Pierre.

  3. #3
    Join Date
    Feb 2004
    Posts
    4
    Isn't it simpler :

    for file in `ls -lt ./module/src/`
    do
    basename $file
    done
    or in one line :
    for file in `ls -lt ./module/src/`;do basename $file ; done

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    That doesn't work for the result of a 'ls -lt' command, because the result contains not only the filename but also perms, owner, group, date.

    For a 'ls -t' command, it's ok.
    Jean-Pierre.

Posting Permissions

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