removing frame charecters

Hi
I have a requirement as follows. My Input file is as follows.

COL1,COL2,COL3,COL4,COL5
987,2,3~7~5,400~468~598,0005~4687~5980
1111,2,2~7,400~468,0005~897

Expected OUTPUT

COL1,COL2,COL3,COL4,COL5
987,2,3,400,0005
987,2,7,468,4687
987,2,5,598,5980
1111,2,2,400,0005
1111,2,7,468,897

COL3,COL4 and COL5 are in turn having own sub division with ~ as delimiter. COL3 can have any number of records till 80. If COL3 has 4 records then COL4 and COL5 contains same number of records.That is COL3 is dynamic.This can be done by counting and cutting columns in loop. But,How can this be done using some array structure to make it faster.

This should do the job:

awk ' BEGIN{FS=OFS=","}
{
  n=split($3,a1,"~")
  split($4,a2,"~")
  split($5,a3,"~")
  for(i=1;i<=n;i++) {
    print $1,$2,a1,a2,a3
  }
}' file

Regards

thanks Buddy..that works
I know FS which is file seperator.

  1. Can you explain me the key words like OFS.
  2. how does this works for each line in a file..there are no loops for going to next line.

OFS is the output field separator. Simply you can see an awk script as a loop, it executes the instructions for each line of the input and after the last instruction it reads the next line and so on until the end of the file. Google for "awk tutorial".

Regards

RD=`date +%Y"-"%m"-"%d" "%T`
awk ' BEGIN{FS=OFS=","}
{
n=split($3,a1,"~")
split($4,a2,"~")
split($5,a3,"~")
for(i=1;i<=n;i++) {
print $1,$2,a1[i],a2[i],a3[i],$RD
}
}' file

I am writing this inside an shell script.In the above code if I add RD inside awk , its calling the value.How can I call the external value inside awk. i have bolded it.

Replied in calling external values inside awk command - Page 2

Double post questions is not allowed, please read the:

Continue here:

http://www.unix.com/unix-advanced-expert-users/61313-calling-external-values-inside-awk-command.html\#post302185832

Thread closed.