Warm tip: This article is reproduced from serverfault.com, please click

How can I split a splited field in AWK?

发布于 2020-11-30 09:26:46

I'm developing a script that has to take a text file with the format: field1@field2@20200101133030@field4 and I need to convert it into a sql insert command. Well. The third field is a date. I have a script that converts the date into to_date('2020/01/01:13:30:30', 'yyyy/mm/dd:hh24:mi:ss'),

funcion(){
        echo $1 | nawk -v s1="\047" -v firstPart="to_date(" -v lastPart="yyyy/mm/dd:hh24:mi:ss" '
                {
                        print firstPart s1 substr($0,1,4) "/" substr($0,5,2) "/" substr($0,7,2)":"\
                        substr($0,9,2)":"substr($0,11,2)":"substr($0,13,2) s1 ", " s1\
                        lastPart s1 ")"
                }'
}

And I have another script that splits the whole line into a insert command:

createInsert(){
        echo $1 | awk -F'@' '{
        printf "INSERT INTO myTable VALUES (\x27%s\x27,\x27%s\x27,\x27%s\x27,\x27%s\x27,\x27%s\x27,\x27%s\x27,\x27%s\x27);",$1,$2,$3,$4,$5,$6,$7;print ""
        }'
}

That works OK. Output:

INSERT INTO myTable VALUES ('0000000','D00','20200901131010','20200917133115','20200917142400','20200923120401','N');

My issue is that I need to convert the $3, $4 and $5 fields using the function below. But I think that awk doesn't allow to call another function like

createInsert(){
        echo $1 | awk -F'@' '{
        printf "INSERT INTO myTable VALUES (\x27%s\x27,\x27%s\x27,\x27%s\x27,\x27%s\x27,\x27%s\x27,\x27%s\x27,\x27%s\x27);",$1,$2,**$(funcion $3)**,$4,$5,$6,$7;print ""
        }'
}

Is there any form to create One single method that splits the fields $3, 4 and 5 and does the same that the other functions does (create it into a toDate function)

Thank you, hope your answers.

SUMMARY: Input:

`"0000000@D01@000000@20200910103010@20200910111111@20200910100403@N@"` 

Output:

INSERT INTO myTable VALUES ('0000000','D01','000000','to_date('2020/09/10:10:30:10', 'yyyy/mm/dd:hh24:mi:ss')',''to_date('2020/09/11:11:11:11', 'yyyy/mm/dd:hh24:mi:ss')','to_date('2020/10/10:04:03', 'yyyy/mm/dd:hh24:mi:ss')','N');
Questioner
Vazzattacc
Viewed
0
RavinderSingh13 2020-11-30 18:25:30

Could you please try following, this is completely based on shown samples of OP in GNU awk. In case your field numbers are different than shown samples then try changing field numbers in solution and it should work for you, I also considered that your shown value 7243176 we need to hardcode if that's not the case then you could put respective field number there to get it.

awk -v s1="'" '
BEGIN{
  FS="@"
  OFS="\047, \047"
}
{
  print "INSERT INTO myTable VALUES(\0477243176",$2,$1,\
  "to_date(\047" substr($4,1,4)"/"substr($4,5,2)"/"\
  substr($4,7,2)":"substr($4,9,2)":"substr($4,11,2)":"\
  substr($4,13,2),"yyyy/mm/dd:hh24:mi:ss\047)","\
  to_date(\047" substr($5,1,4)"/"substr($5,5,2)"/"\
  substr($5,7,2)":"substr($5,9,2)":"substr($5,11,2)\
  ":"substr($5,13,2),"yyyy/mm/dd:hh24:mi:ss\047)\
  " ",\047N\047)"
}
'  Input_file

Also considering that " is not part of your sample input in case it is then you could use sub function to substitute it inside main block of awk program.