Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk Question

Status
Not open for further replies.

MotorCycleDude

Technical User
Sep 7, 2023
6
US
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.
 
The error is invalid regexp.
If your regexp is
[pre]/[BUBBA/[/pre]
then it is invalid.
What do you want to match ?
If BUBBA or [BUBBA then try to use the regex
[pre]/[\[]*BUBBA/[/pre]
 
microm,

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
 
your error says that in your script on line 232
is this error
[pre]invalid regexp: Unmatched [, [^, [:, [., or [=: /[BUBBA/[/pre]

Show, what regular expression you have on line 232
 
Here’s the surrounding lines. It’s in the middle with the “sub” call.
This worked back in the early 90’s when I wrote it.
.
.
.
[pre][/pre] }
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;
}
.
.
.
 
I could simulate your error with this little script
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) 
  print $0        
}

when I ran it on your text file
MotorCycleDude.txt
Code:
apples peaches BUBBA pie
apples peaches BUBBA pie
apples peaches [BUBBA pie
apples peaches BUBBA pie
apples peaches BUBBA pie
I get the same error:
Code:
$ awk -f MotorCycleDude.awk MotorCycleDude.txt
apples peaches foo pie
apples peaches foo pie
awk: MotorCycleDude.awk:12: (FILENAME=MotorCycleDude.txt FNR=3) fatal: invalid regexp: Unmatched [, [^, [:, [., or [=: /[BUBBA/
 
the problem is with the function
[pre]sub(regex, replacement)[/pre]
which expects as the first argument regexp and [BUBBA is ilegal regexp, because [ is in regexps reserved symbol, which must be paired with ]
 
Instead of using the function sub()
[pre]sub($col_to_chg, to)[/pre]
use simple assigment:
[pre]$col_to_chg = to[/pre]

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"  
}

{
[highlight #FCE94F]  #sub($col_to_chg, to)
  $col_to_chg = to[/highlight]
  print $0        
}

Running it gives me this results:
Code:
$ awk -f MotorCycleDude.awk MotorCycleDude.txt
apples peaches foo pie
apples peaches foo pie
apples peaches foo pie
apples peaches foo pie
apples peaches foo pie

 
microm,
You are a lifesaver!
That works like a charm.
I can’t thank you enough.
Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top