regex - AWK: Using a variable as part of a regular expression -
this question has answer here:
- how use shell variables in awk script? 6 answers
i have text file similar 1 containing username, description , 2 time range values german date format:
user###@###description###@###1. august - 8. august 2016###@###1. september - 7. september 2016
each field gets separated using ###@###
delimiter. check if field (e.g. $3) contains 2 identical month names. if there 2 month names in specified field, first month name should removed, output of awk is:
user###@###description###@###1. - 8. august 2016###@###1. - 7. september 2016
then got idea create for-loop bash script (with awk commands), increments i
in order read out month name predefined variable. here can more detailed
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 unable search varname mi (like m1, m2, m3.. etc.)
what have change in order search variable pattern actions?
you can put predefined names in awk script. this, maybe. (quick hack - log off day ;) )
awk -f ... ' begin { m[1]="january"; m[2]="february"; ... } {for(i=1... if ( $3 ~ m[i] ) { count++ } ...}'
edit: benefit of future readers, here's text op's shorttext.com link below:
awk -f '###@###' ' begin{m1="januar"; m2="february"; m3="march"; m4=april; m5=may; m6=june; m7=july; m8=august; m9=september; m10=october; m11=november; m12=december} {for (i in m){ count=0; $3 ~ (m[i] ".*" m[i]) {print ++count}; if (count == 1){sub(m[i],"" ,$3)} }}' info.txt > info.tmp
Comments
Post a Comment