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
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 "_"
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
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:
Summary: Given a phrase like this: WORD1 WORD2: WORD3 WORD4 WORD5 I just want WORD3 WORD4 WORD5. I can tail the file it comes from to get the last line, but how do I get only those last few words? It needs ...
Summary: Hi, I have file like this: ====================== 1232 asdasds: 121 wefefsedfsd asdsd !34234 sdfdf 43423 fdsfsd 423434 end =================== I would like to remove the first field which start wi...
Summary: Any way of getting the modified timestamp of a file in the format I want, say dd/mm/yyyy hh24:mi:ss format, irrespective of when the file is last modified. I'm working on SunOS 5.5.1 Thanks. ...