Home > Archive > AWK > May 2006 > square brackets problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
square brackets problem
|
|
| Dave.Kritzberg@gmail.com 2006-05-16, 3:56 am |
| Hello regular expression experts,
I am trying to clean up a data file, using a combination of sed and
awk in a bash shell script. I'm a beginner at both, and I'm stuck
on a particular expression.
In a few records in the data, a field has the literal value [NULL] --
which I would like to replace with 0 (zero). However, [ and ]
comprise a regular expression, and \[ \] are not accepted as an escape
in either awk or sed, apparently, based on warnings I'm receiving at
the command line. I have tried it this way in sed:
s/[NULL]/0/g
.... and this way in awk:
BEGIN {FS=OFS="\t"}
{
{ gsub("[NULL]","0",$22)
print }
}
END {}
or:
BEGIN {FS=OFS="\t"}
{
{ gsub("\[NULL\]","0",$22)
print }
}
END {}
None of these work. Can anyone offer me a way to fix this?
Thanks in advance,
Dave
--
Dave Kritzberg
University of Colorado Economics
dijon.colorado.edu
| |
| Kenny McCormack 2006-05-16, 3:56 am |
| In article <1147752158.723592.243110@j33g2000cwa.googlegroups.com>,
<Dave.Kritzberg@gmail.com> wrote:
>Hello regular expression experts,
>
>I am trying to clean up a data file, using a combination of sed and
>awk in a bash shell script. I'm a beginner at both, and I'm stuck
>on a particular expression.
Lose the "combination of sed and awk". You never need sed, once you've
taken the step forward to awk.
>In a few records in the data, a field has the literal value [NULL] --
>which I would like to replace with 0 (zero). However, [ and ]
>comprise a regular expression, and \[ \] are not accepted as an escape
>in either awk or sed, apparently, based on warnings I'm receiving at
>the command line.
>
>BEGIN {FS=OFS="\t"}
>{
Lose this.
> { gsub("\[NULL\]","0",$22)
{ gsub(/\[NULL\]/,0,$22)
> print }
>}
Lose this.
>END {}
Lose this.
>None of these work. Can anyone offer me a way to fix this?
Basically, you should be using /'s to enclose REs, not "s.
(I will leave it others to dot the Is and cross the Ts as to why this is so)
Alternatively, you could try [[] and []].
| |
| Robert Katz 2006-05-16, 3:57 am |
| Dave.Kritzberg@gmail.com wrote:
> Hello regular expression experts,
>
> I am trying to clean up a data file, using a combination of sed and
> awk in a bash shell script. I'm a beginner at both, and I'm stuck
> on a particular expression.
>
> In a few records in the data, a field has the literal value [NULL] --
> which I would like to replace with 0 (zero). However, [ and ]
> comprise a regular expression, and \[ \] are not accepted as an escape
> in either awk or sed, apparently, based on warnings I'm receiving at
> the command line. I have tried it this way in sed:
>
> s/[NULL]/0/g
sed 's/\[NULL]/0/'
>
> ... and this way in awk:
>
> BEGIN {FS=OFS="\t"}
> {
> { gsub("[NULL]","0",$22)
> print }
> }
> END {}
>
> or:
>
> BEGIN {FS=OFS="\t"}
> {
> { gsub("\[NULL\]","0",$22)
> print }
> }
> END {}
>
> None of these work. Can anyone offer me a way to fix this?
>
awk 'sub(/\[NULL]/, "0")1'
--
Regards,
---Robert
|
|
|
|
|