how to get the last field
|
Original Message
|
Name: mehulnp
Date: October 20, 2004 at 13:04:03 Pacific
Subject: how to get the last fieldOS: unixCPU/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
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Jim Boothe
Date: October 20, 2004 at 14:26:35 Pacific
Subject: how to get the last field |
Reply: (edit)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
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: mehulnp
Date: October 20, 2004 at 14:45:52 Pacific
Subject: how to get the last field |
Reply: (edit)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
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: nails
Date: October 20, 2004 at 15:01:25 Pacific
Subject: how to get the last field |
Reply: (edit)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
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: Jim Boothe
Date: October 20, 2004 at 15:09:17 Pacific
Subject: how to get the last field |
Reply: (edit)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.
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: frank gerigk
Date: October 21, 2004 at 03:12:27 Pacific
Subject: how to get the last field |
Reply: (edit)to avoid awk just use: { while read line do LAST=`echo ${line##*_}` done } < your_file LAST will have the last field.
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: thepubba
Date: October 21, 2004 at 06:33:23 Pacific
Subject: how to get the last field |
Reply: (edit)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
Report Offensive Follow Up For Removal
|
Use following form to reply to current message: