Home > Blockchain >  Need help in converting 'while-do' block to 'awk' block for quicker processing
Need help in converting 'while-do' block to 'awk' block for quicker processing

Time:02-24

I need 7th field of a csv file converted from julian(yyddd or yyJJJ) to yyyymmdd. I have the below while do loop. I need the same logic using awk command for quicker processing. Can someone help ?

count=0
while read -r line1; do
        col_7=$( echo $line1 | cut -d ',' -f7 | cut -c4-6)
        year1=$( echo $line1 | cut -d ',' -f7 | cut -c2-3)
        echo $col_7
        col_1=$( echo $line1 | cut -d ',' -f1,2,3,4,5,6)
        col_8=$( echo $line1 | cut -d ',' -f8 )
        date7=$(date -d "01/01/${year1}  ${col_7} days -1 day"  %Y%m%d)
        echo $date7
        echo $col_1,$date7,$col_8 >> ${t2}
        count=$[count 1]
done < ${t1}

Input
xx,x,x,xxx,xxxx,xxxxx,021276,x
xx,x,x,xxx,xxxx,xxxxx,021275,x
xx,x,x,xxx,xxxx,xxxxx,021275,x

Output
xx,x,x,xxx,xxxx,xxxxx,20211003,x
xx,x,x,xxx,xxxx,xxxxx,20211002,x
xx,x,x,xxx,xxxx,xxxxx,20211002,x

CodePudding user response:

Just eliminating all the calls to cut will do wonders; you may not need awk.

count=0
while IFS=, read -r c1 c2 c3 c4 c5 c6 c7 col_8 rest; do
    col_7=${c7:3:3}
    year1=${c7:1:2}
    col_1=$c1$c2$c3$c4$c5$c6
    col_8=$c8
    date7=$(date -d "01/01/$year1  $col_7 days - 1 day"  %Y%m%d)
    ...
    count=$((count 1))
done < "$t1"

CodePudding user response:

Here is a solution for awk. Tested it on terminal, so it is pretty much a one-liner command.

awk 'BEGIN { FS=OFS="," } { $7=strftime("%Y%m%d",mktime("20"substr($7,2,2)" 01 01 00 00 00") (substr($7,4)*24*60*60)-3600) } 1' filename.txt
  • Related