Otaku Kyon Otaku Kyon - 2 months ago 11
Bash Question

AWK: Search pattern is a variable

I have a text file similar to this one containing a username, a description and two time range values with German date format:

User###@###Description###@###1. August - 8. August 2016###@###1. September - 7. September 2016


Each field gets separated using the
###@###
delimiter. I would like to check if a certain field (e.g. $3) contains two identical month names. If there are two month names in this specified field, the first month name should get removed, so that the output of awk is:

User###@###Description###@###1. - 8. August 2016###@###1. - 7. September 2016


Then I got the idea to create a for-loop for my bash script (with awk commands), which increments
i
in order to read out the month name from a predefined variable. Here you can get a more detailed look

script.sh:

m1=January; m2=February; m3=March; m4=April; m5=May; m6=June; m7=July; m8=August; m9=September; m10=October; m11=November; m12=December


awk -F '###@###' '
{for (i=1;i++;i<=12){
count=0;
$3 ~ 'm'i {count++};
if (count == 2){gsub(mi,"" ,$3)}
}}' Info.txt > Info.tmp


Unfortunately it is unable to search for the varname mi (like m1, m2, m3.. etc.)

What do I have to change in order to search a variable with a certain pattern to do some actions?

cxw cxw
Answer

You can put the predefined names in the awk script. Something like this, maybe. (Quick hack - just about to log off for the day ;) )

awk -F ... ' BEGIN { m[1]="January"; m[2]="February"; ... } 
            {for(i=1...
             if ( $3 ~ m[i] ) { count++ }
             ...}'
Comments