×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

awk Question

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.

RE: awk Question

The error is invalid regexp.
If your regexp is
/[BUBBA/ 
then it is invalid.
What do you want to match ?
If BUBBA or [BUBBA then try to use the regex
/[\[]*BUBBA/ 

RE: awk Question

(OP)
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

RE: awk Question

your error says that in your script on line 232
is this error
invalid regexp: Unmatched [, [^, [:, [., or [=: /[BUBBA/ 

Show, what regular expression you have on line 232

RE: awk Question

(OP)
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.
.
.
.
 
}
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

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/ 

RE: awk Question

(OP)
Nice to know I’m not the only one! Thanks.

RE: awk Question

the problem is with the function
sub(regex, replacement) 
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

Instead of using the function sub()
sub($col_to_chg, to) 
use simple assigment:
$col_to_chg = to 

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

$ 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 

RE: awk Question

Does it work now for you ?

RE: awk Question

(OP)
Great. I’ll give that a try ASAP!

RE: awk Question

(OP)
microm,
You are a lifesaver!
That works like a charm.
I can’t thank you enough.
Matt

RE: awk Question

You are welcome.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close