I have this code in a test.awk file:
{FS=","} {gsub(/\/[0-9]{1,2}\/[0-9]{4}/,"");}1
{FS=","} {gsub(/:[0-9][0-9]/,"");}1
The code makes transformations in a dataset from a dataset.csv file. I want that using the following command at the shell, returns me a newdataset.csv with all the modifications:
gawk -f test.awk dataset.csv
CodePudding user response:
Put both commands in the same block.
BEGIN {FS=","}
{ gsub(/\/[0-9]{1,2}\/[0-9]{4}/,"");
gsub(/:[0-9][0-9]/,"");
}1
You could also do them in the same regexp with alternation, since the replacement is the same.
And since you never do anything that operates on individual fields, there's no need to set the field separator.
{gsub(/:[0-9][0-9]|\/[0-9]{1,2}\/[0-9]{4}/, "")}1
CodePudding user response:
@bramaawk : what this -
echo 'abc:12/31/2046def' |
awk '{gsub(/:[0-9][0-9]|\/[0-9]{1,2}\/[0-9]{4}/, "")}1'
.. looks like to any awk
is -
abcdef
# gawk profile, created Sun Jun 5 10:59:06 2022
# Rule(s)
1 {
1 gsub(/:[0-9][0-9]|\/[0-9]{1,2}\/[0-9]{4}/,"",$0)
}
1 1 { # 1
1 print
}
what I'm suggesting is to streamline those into 1 single block :
awk 'gsub(":[0-9]{2}|[/][0-9]{1,2}[/][0-9]{4}",_)^_'
so that awk
only needs to see :
# gawk profile, created Sun Jun 5 10:59:34 2022
# Rule(s)
1 gsub(":[0-9]{2}|[/][0-9]{1,2}[/][0-9]{4}",_,$0)^_ { # 1
1 print
}
instead of 2 boolean evaluations
(or the poorly-termed "patterns
"), and 2 action blocks
, make them 1 each instead.
To make your solution generic for gawk mawk nawk
, just do
{m,n,g}awk NF FS=':[0-9][0-9]|[/][0-9][0-9]?[/][0-9][0-9][0-9][0-9]' OFS=