I am new to unix shell scripting. I need to parse fixed length data file and convert to comma delimeter. I manage to do this. By using code below:
awk '{
one=substr($0,1,1)
two=substr($0,2,10)
three=substr($0,12,4)
four=substr($0,16,2)
rest=substr($0,18)
printf ("%s,%s,%s,%s,%s\n", one, two, three, four, rest)
}' data.txt > out.txt
Data.txt:
k12582927001611USNA
k12582990001497INAS
k12583053001161LNEU
Output.txt:
k,1258292700,1611,US,NA
k,1258299000,1497,IN,AS
k,1258305300,1161,LN,EU
The problem is I have a requirement to read the column position from a config file.
My configfile (configfile.txt) as below:
one=substr($0,1,1)
two=substr($0,2,10)
three=substr($0,12,4)
four=substr($0,16,2)
rest=substr($0,18)
To meet the requirement, I have created script as below:
configparam=`cat configfile.txt`
awk '{
$configparam
printf ("%s,%s,%s,%s,%s\n", one, two, three, four, rest)
}' data.txt > out.txt
but its not working. Can anybody here show me the correct way to achieve this?