Last element from cut


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Last element from cut
# 1  
Old 07-05-2005
Last element from cut

Hi,

I am looking for a way to get the last element of a cut command

ex.

$pwd
/remote/username/dir1/dir2/dir3

$pwd | cut -f3 -d/
username

I need to extract "dir3" (last one) no matter whichever position it is.

Thanks,
sskb
# 2  
Old 07-05-2005
Use

Code:
basename /remote/username/dir1/dir2/dir3

or if you need to use pwd

Code:
basename `pwd`

Vino
# 3  
Old 07-05-2005
Or u can use awk

awk -F"/" '{ print $NF }'
# 4  
Old 07-05-2005
Quote:
Originally Posted by rahul123_libra
awk -F"/" '{ print $NF }'

More clearly, if you are using awk you should be doing it like this.

Code:
echo /remote/username/dir1/dir2/dir3 | awk -F"/"  '{ print  $NF }'

There is a small downfall to using awk in this case. Depends on the input you give.
Code:
echo /remote/username/dir1/dir2/dir3/ | awk -F"/"  '{ print  $NF }'

would give a blank i.e. no useful output will you receive.

vino
 
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. Shell Programming and Scripting

Print the row element till the next row element appear in a column

Hi all I have file with columns F3 pathway CPS F2 H2 H4 H5 H6 no pathway CMP H7 H8 H9 H10 My expected output is F3 pathway CPS F2 pathway CPS (10 Replies)
Discussion started by: Priyanka Chopra
10 Replies

4. Shell Programming and Scripting

Find if XML element has a matching required element

I want to check if every <Part> element has corresponding <Description> in this sample XML. ....<Lot Of XML> <Inv lineNumber="2"> <Item> ... (4 Replies)
Discussion started by: kchinnam
4 Replies

5. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies
Login or Register to Ask a Question