awk Question
awk Question
(OP)
I have a ‘relatively’ simple awk script that changes ‘columns’ (delimited by whitespaces) in an ascii text file. It is sound and works until you introduce a special character like “(“ or “[“. It then results in the error message below and doesn’t make the change. I’ve Googled and Googled and Googled, but no solution. Will anyone help? TIA , Matt
Here’s what I get if one of the columns contains “[“ or “(“
change_by_col abc -c 3 -t BARNEY
awk: /home/matt/Software/Scripts/change_by_col:232: (FILENAME=abc FNR=3) fatal: invalid regexp: Unmatched [, [^, [:, [., or [=: /[BUBBA/
Here’s the text file:
apples peaches BUBBA pie
apples peaches BUBBA pie
apples peaches [BUBBA pie
apples peaches BUBBA pie
apples peaches BUBBA pie
Here’s the line that errors out (232)
sub($col_to_chg,to);
(btw, “to” above is what’s assigned from the “-t” argument.
Here’s what I get if one of the columns contains “[“ or “(“
change_by_col abc -c 3 -t BARNEY
awk: /home/matt/Software/Scripts/change_by_col:232: (FILENAME=abc FNR=3) fatal: invalid regexp: Unmatched [, [^, [:, [., or [=: /[BUBBA/
Here’s the text file:
apples peaches BUBBA pie
apples peaches BUBBA pie
apples peaches [BUBBA pie
apples peaches BUBBA pie
apples peaches BUBBA pie
Here’s the line that errors out (232)
sub($col_to_chg,to);
(btw, “to” above is what’s assigned from the “-t” argument.
RE: awk Question
If your regexp is
then it is invalid.
What do you want to match ?
If BUBBA or [BUBBA then try to use the regex
RE: awk Question
In the change_by_col awk script I want to change column 3 to another word (you choose…).
It errors on “[BUBBA” .
How do I get my script to “ignore” the “[“ ? Or is that not possible within the awk script?
btw, here is where I try to make the substitution:
sub($col_to_chg,to);
Thanks
RE: awk Question
is this error
Show, what regular expression you have on line 232
RE: awk Question
This worked back in the early 90’s when I wrote it.
.
.
.
}
else { # no keyword, global sub
check_end_int = index(line[NR],"(");
if (check_end_int > 0) {
printf("Found a paren.\n");
}
else {
sub($col_to_chg,to);
}
}
line[NR] = $0;
}
else {
line[NR] = $0;
}
.
.
.
RE: awk Question
MotorCycleDude.awk
CODE
when I ran it on your text file
MotorCycleDude.txt
CODE
CODE
RE: awk Question
RE: awk Question
which expects as the first argument regexp and [BUBBA is ilegal regexp, because [ is in regexps reserved symbol, which must be paired with ]
RE: awk Question
use simple assigment:
Here is the working example
MotorCycleDude.awk
CODE
BEGIN { # column number to change col_to_chg = 3 # content of column $col_to_chg to to = "foo" } { #sub($col_to_chg, to) $col_to_chg = to print $0 }
Running it gives me this results:
CODE
RE: awk Question
RE: awk Question
RE: awk Question
You are a lifesaver!
That works like a charm.
I can’t thank you enough.
Matt
RE: awk Question