split file depending on content

Hi,
I have a file which contains records of data.
I need to split the file into multiple files depending upon the value of last field.
How do i read the last field of each record in the file???

Regards,
Chaitrali

what is the record separator in your file?

give some sample data in order to receive good responses

Hi Yogesh,

The record is tab separated......

Rgds,
Chairali.

In this case the last field would be matched by a regexp (replace "<tab>" by a literal tab character):

sed 's/.*<tab>//' file

The reason why this works is because Unix regexps are "greedy": always the longest possible match is used. If you have several tabs in one input line ".*<tab>" will match all possible characters (including tabs!) followed by a tab char, which will be the rightmost one. Be sure to have no trailing tabs at the end of the line, though, as in this case the regexp would match the whole line.

To extract the rightmpost field from the line and split the file linewise into different files use something like:

cat file | while read line ; do
     lastfield="$(print $line | sed 's/.*<tab>//')"
     case $lastfield in
          abc)
               print - "$line" >> file1
               ;;

           def)
               print - "$line" >> file2
               ;;

           ghi)
               print - "$line" >> file3
               ;;

           *)
               print -u2 "i do not know where to put $line"
               ;;

     esac
done     

bakunin

awk -F"\t" '{ print $NF }' filename

to split files based on the last field value using the last field value as the split_filename

awk -F"\t" '{ print > $NF }' filename