If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Cut the Last Field

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-25-04, 03:47
murali_vijayan murali_vijayan is offline
Registered User
 
Join Date: Feb 2004
Location: India
Posts: 1
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.
Reply With Quote
  #2 (permalink)  
Old 02-25-04, 04:22
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #3 (permalink)  
Old 02-29-04, 18:31
arik.fish arik.fish is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 03-01-04, 03:36
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On