Hello does anyone know how to do the following using some simple unix if possible:I have a file with the following info as comma separated. It shows username, id number and what subjects they are enrolled in. The subjects are separated by colons. UserName,ID,Subjectjsmith,235456939,MATH29879:PHYS45979jdoe,750378929,ENGL36899:JAPN92576I want it to have one username with one course per line. So that it would look like:jsmith,235456939,MATH29879jsmith,235456939,PHYS45979jdoe,750378929,ENGL36899jdoe,750378929,JAPN92576Does anyone have any ideas?Thnx-J

Questions by jennyp

Showing Answers 1 - 24 of 24 Answers

Hi

Try following

1) Say your file name is "r1" having following data

STUDENT1,ID1,SUB11
STUDENT2,ID2,SUB21:SUB22
STUDENT3,ID3,SUB31:SUB32:SUB33
STUDENT4,ID4,SUB41:SUB42:SUB44

2)Now write following in a new file "prg.awk"

{FS=","}
{NAME=$1;ID=$2;SUBJECT=$3}
{split(SUBJECT,SUB,":")
{printf "%s,%s,%sn",NAME,ID,SUB[1]}
{if(SUB[2]!=NULL){printf "%s,%s,%sn",NAME,ID,SUB[2]}
{if(SUB[3]!=NULL){printf "%s,%s,%sn",NAME,ID,SUB[3]}
{if(SUB[4]!=NULL){printf "%s,%s,%sn",NAME,ID,SUB[4]}
{if(SUB[5]!=NULL){printf "%s,%s,%sn",NAME,ID,SUB[5]}

3)Make this file as executable, say chmod +x prg.awk

4)Now write the following command

awk -f prg.awk r1

You can add more line with SUB[#] depending of number of subjects


Regards
RITESH

  Was this answer useful?  Yes

Muthukumar

  • Mar 3rd, 2007
 

Script Starts here

### Usage : sh format.sh inputFile.txt

if [ $# -ne 1 ] ;
then
        echo "Usage : sh format.sh inputFile.txt";
        exit 1;
fi
FNAME=$1
CONTENT=`cat $FNAME`;

for LINE in $CONTENT
do
        NAME=`echo $LINE | awk -F, '{print $1}'`;
        ID=`echo $LINE | awk -F, '{print $2}'`;
        SUBJECTS=`echo $LINE | awk -F, '{print $3}' | sed 's/:/ /g'`;
        for SUBS in $SUBJECTS
        do
                echo "$NAME,$ID,$SUBS";
        done
done

Script Ends with that done

Create it as script with name like format.sh and pass your file as argument.
it will output the data in your desired format

Regards
Muthu

  Was this answer useful?  Yes

cmkumar_ea

  • Mar 3rd, 2007
 

Script starts here

### Usage : sh format.sh inputFile.txt

if [ $# -ne 1 ] ;
then
        echo "Usage : sh format.sh inputFile.txt";
        exit 1;
fi
FNAME=$1
CONTENT=`cat $FNAME`;

for LINE in $CONTENT
do
        NAME=`echo $LINE | awk -F, '{print $1}'`;
        ID=`echo $LINE | awk -F, '{print $2}'`;
        SUBJECTS=`echo $LINE | awk -F, '{print $3}' | sed 's/:/ /g'`;
        for SUBS in $SUBJECTS
        do
                echo "$NAME,$ID,$SUBS";
        done
done

Script ends here

save it as format.sh and pass your input as .txt file
you can see the output in the desired format.

  Was this answer useful?  Yes

steven Li

  • May 9th, 2007
 

$ cat input.file

jsmith,235456939,MATH29879:PHYS45979

jdoe,750378929,ENGL36899:JAPN92576


$ sed -e 's/:[A-Z]*[1-9]*//g' input.file>output.file


$ cat output.file

jsmith,235456939,MATH29879

jdoe,750378929,ENGL36899

  Was this answer useful?  Yes

use below command:

$ cat t.txt
jsmith,235456939,MATH29879:PHYS45979
jdoe,750378929,ENGL36899:JAPN92576

$ cat t.txt|sed "s/:/,/g"|awk -F"," '{OFS=","; print $1,$2,$3;print $1,$2,$4}'
jsmith,235456939,MATH29879
jsmith,235456939,PHYS45979
jdoe,750378929,ENGL36899
jdoe,750378929,JAPN92576

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions