With a FS of 2 characters or more awk leaves --csv mode
wajap opened this issue · comments
Wilbert van der Poel commented
Normal behavior: echo 'aa,"bb"' | ./nawk_org --csv '{print $1 "|" $2}'
gives aa|bb
But: echo 'aa,"bb"' | ./nawk_org -Fbb --csv '{print $1 "|" $2}'
gives aa,"|"
and no warning
Also: echo 'aa,"bb"' |/nawk_org --csv 'BEGIN{FS="bb"}{print $1 "|" $2}'
gives:
./a.out: danger: don't set FS when --csv is in effect source line number 1
aa,"|"
Below a fix with also a warning when -F is used.
diff -ur org/lib.c new/lib.c
--- org/lib.c 2023-11-24 05:34:46.000000000 +0100
+++ new/lib.c 2023-11-27 13:55:10.000000000 +0100
@@ -399,7 +399,7 @@
i = 0; /* number of fields accumulated here */
if (inputFS == NULL) /* make sure we have a copy of FS */
savefs();
- if (strlen(inputFS) > 1) { /* it's a regular expression */
+ if (!CSV && strlen(inputFS) > 1) { /* it's a regular expression */
i = refldbld(r, inputFS);
} else if (!CSV && (sep = *inputFS) == ' ') { /* default whitespace */
for (i = 0; ; ) {
diff -ur org/main.c new/main.c
--- org/main.c 2023-11-24 05:34:46.000000000 +0100
+++ new/main.c 2023-11-27 13:55:10.000000000 +0100
@@ -157,6 +157,8 @@
}
if (strcmp(argv[1], "--csv") == 0) { /* turn on csv input processing */
CSV = true;
+ if (fs)
+ WARNING("danger: don't set FS when --csv is in effect");
argc--;
argv++;
continue;
@@ -178,6 +180,8 @@
break;
case 'F': /* set field separator */
fs = setfs(getarg(&argc, &argv, "no field separator"));
+ if (CSV)
+ WARNING("danger: don't set FS when --csv is in effect");
break;
case 'v': /* -v a=1 to be done NOW. one -v for each */
vn = getarg(&argc, &argv, "no variable name");
diff -ur org/testdir/T.csv new/testdir/T.csv
--- org/testdir/T.csv 2023-11-24 05:34:46.000000000 +0100
+++ new/testdir/T.csv 2023-11-27 13:55:10.000000000 +0100
@@ -17,7 +17,7 @@
sub(/try /, "")
prog = $0
printf("%3d %s\n", nt, prog)
- prog = sprintf("%s -F\"\\t\" '"'"'%s'"'"'", awk, prog)
+ prog = sprintf("%s '"'"'%s'"'"'", awk, prog)
# print "prog is", prog
nt2 = 0
while (getline > 0) {
ozan (oz) s. yigit commented
good catch, thanks.
ozan (oz) s. yigit commented
your fix included in the latest edition.
Wilbert van der Poel commented
When FS is set with the -v option there is no warning
The change below fixes this.
diff -au org/main.c new/main.c
--- org/main.c 2023-11-27 20:31:28.000000000 +0100
+++ new/main.c 2023-12-09 11:24:04.323724500 +0100
@@ -157,8 +157,6 @@
}
if (strcmp(argv[1], "--csv") == 0) { /* turn on csv input processing */
CSV = true;
- if (fs)
- WARNING("danger: don't set FS when --csv is in effect");
argc--;
argv++;
continue;
@@ -180,8 +178,6 @@
break;
case 'F': /* set field separator */
fs = setfs(getarg(&argc, &argv, "no field separator"));
- if (CSV)
- WARNING("danger: don't set FS when --csv is in effect");
break;
case 'v': /* -v a=1 to be done NOW. one -v for each */
vn = getarg(&argc, &argv, "no variable name");
@@ -203,6 +199,8 @@
argc--;
argv++;
}
+ if (CSV && (fs != NULL || lookup("FS", symtab) != NULL))
+ WARNING("danger: don't set FS when --csv is in effect");
/* argv[1] is now the first argument */
if (npfile == 0) { /* no -f; first argument is program */
if (argc <= 1) {
ozan (oz) s. yigit commented
ok, noted, fix pushed.