Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Unix (http://www.go4expert.com/forums/unix-forum/)
-   -   Splitting the records using counter of input file (http://www.go4expert.com/forums/splitting-records-using-counter-input-t27725/)

bashamsc 4Feb2012 07:18

Splitting the records using counter of input file
 
I am having a File format as mentioned below

Quote:

Employee id|Name|Languages

12345|ABC|02|English|Hindi
4567|DEF|03|Engligh|Hindi|Urdu

the 02 and 03 are counters. Using that we need to generate the output records as mentioned below

12345|ABC|English
12345|ABC|Hindi

4567|DEF|English
4567|DEF|Hindi
4567|DEF|Urdu
Below is the code to handle above scenario

Code:

while read line   
do   
        emp_id=`echo $line | awk -F'|' '{print $1}'`
        name=`echo $line | awk -F'|' '{print $2}'`

        counter=`echo $line | awk -F'|' '{print $3}'`
        counter1=4

        while [[ $counter -gt 0 ]] 
        do
                                                                         
                language=`echo $line |cut -f$counter1 -d"|"`
                                                                       
                counter1=`expr $counter1 + 1`
                counter=`expr $counter - 1`
                                                                       
                echo $emp_id'|'$name'|'$language>>output.txt
        done


done <input.txt



All times are GMT +5.5. The time now is 20:12.