Computing.Net > Forums > Unix > how to get the last field

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

how to get the last field

Reply to Message Icon

Name: mehulnp
Date: October 20, 2004 at 13:04:03 Pacific
OS: unix
CPU/Ram: unix
Comment:

Hello,

how do i get the last field
Ex: files are with this formats
abc_xyz_20041232
abc_20041001
abbc_xyz_rrr_20041013

i tried with cut command but i doono how to get the last filed the file names has many dilimiters "_"

any help appreciated

Thanks
Mehul



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: October 20, 2004 at 14:26:35 Pacific
Reply:

You have these filenames contained in a variable, as if within a for filename loop?

last=$(echo $filename | awk -F_ '{print $NF}')

echo $last
20041232


0

Response Number 2
Name: mehulnp
Date: October 20, 2004 at 14:45:52 Pacific
Reply:

for filename in `cat ./tmp`
do
last=$(echo $filename | awk -F_ '{print $NF}')
done


It gives syntax error
syntax error at line 13: `last=$' unexpected

it works fine with
echo $filename | awk -F_ '{print $NF}'

but not able to store the value


0

Response Number 3
Name: nails
Date: October 20, 2004 at 15:01:25 Pacific
Reply:

Another way to do it:

#!/bin/ksh

for i in abc_xyz_20041232 abc_20041001 abbc_xyz_rrr_20041013
do
set - $(IFS="_"; echo $i)
shift $(($#-1))
var=$1
echo $var
done

Use the set command to parse the string into command line arguments. Then shift the total number of variables -1 and that sets $1, first argument, equal to the last argument.

Regards,


Nails


0

Response Number 4
Name: Jim Boothe
Date: October 20, 2004 at 15:09:17 Pacific
Reply:

Mehul,

My code requires ksh. If you are not using ksh, then use backquotes instead:

last=`echo $filename | awk -F_ '{print $NF}'`

Let me know how that works.


0

Response Number 5
Name: frank gerigk
Date: October 21, 2004 at 03:12:27 Pacific
Reply:

to avoid awk just use:

{ while read line
do
LAST=`echo ${line##*_}`
done } < your_file

LAST will have the last field.


0

Related Posts

See More



Response Number 6
Name: thepubba
Date: October 21, 2004 at 06:33:23 Pacific
Reply:

And, if you are always chopping off only the last 8 characters:

#!/bin/ksh

typeset -R8 line
exec 3<./data.file

while read -u3 line
do
print $line
done


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: how to get the last field

Get the last few words of a line? www.computing.net/answers/unix/get-the-last-few-words-of-a-line/7459.html

how to remove the first field www.computing.net/answers/unix/how-to-remove-the-first-field/7854.html

how to get the modified time of a file www.computing.net/answers/unix/how-to-get-the-modified-time-of-a-file/2110.html