I am new to unix shell scripting. I need to parse a fixed-length data file and convert to comma. I succeed. Using the following code:
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 that I have a requirement to read a column from a configuration file.
My configuration file (configfile.txt) as shown 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 fulfill this requirement, I created a script as shown below:
configparam=`cat configfile.txt`
awk '{
$configparam
printf ("%s,%s,%s,%s,%s\n", one, two, three, four, rest)
}' data.txt > out.txt
but does not work. Can anyone here show me the correct way to achieve this?