Passing an input parameter from a configuration file in an AWK command

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?

+4
source share
3 answers

The easiest way is to create a file that contains the beginning of the position and the absence of characters, as shown below, you do not need to write so much time one=substr($0,start,n_char);:

Input:

$ cat infile 
k12582927001611USNA
k12582990001497INAS
k12583053001161LNEU

:

$ cat pos 
1,1
2,10
12,4
16,2
18

:

$ awk 'BEGIN{FS=OFS=","}FNR==NR{pos[++i,"s"]=$1;pos[i,"e"]=$2+0?$2:length;next}{for(j=1; j<=i; j++) printf("%s%s", substr($0,pos[j,"s"],pos[j,"e"]),j==i?ORS:OFS)}' pos infile 
k,1258292700,1611,US,NA
k,1258299000,1497,IN,AS
k,1258305300,1161,LN,EU

:

awk 'BEGIN{
            FS=OFS=","
     }
     FNR==NR{
            pos[++i,"s"]=$1;
            pos[i,"e"]=$2+0?$2:length;
            next
     }
     {
          for(j=1; j<=i; j++) 
             printf("%s%s", substr($0,pos[j,"s"],pos[j,"e"]),j==i?ORS:OFS)
     }' pos infile
+1

cat cfg.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)
}

cat printer.awk

{ printf ("%s,%s,%s,%s,%s\n", one, two, three, four, rest) }

awk -f cfg.awk -f printer.awk data.txt

k,1258292700,1611,US,NA
k,1258299000,1497,IN,AS
k,1258305300,1161,LN,EU

, / { .. } ( ) var=substr.

IHTH

+1

The following awk can also help you with this.

awk '
function check(val, re){
  split(val, array,",");
  re=array[1] && array[2]?substr($0,array[1],array[2]):substr($0,array[1]);
  return re
}
FNR==NR{
  match($0,/\(.*\)/);
  a[FNR]=substr($0,RSTART+4,RLENGTH-5);
  count++;
  next}
{
for(i=1;i<=count;i++){
  val=val?val "," check(a[i]):check(a[i])
};
  print val;
  val=""
}
' Input_file_config   Input_file

The output will be as follows.

k,1258292700,1611,US,NA
k,1258299000,1497,IN,AS
k,1258305300,1161,LN,EU
+1
source

Source: https://habr.com/ru/post/1687040/


All Articles