LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk warning on escape sequence (https://www.linuxquestions.org/questions/linux-newbie-8/awk-warning-on-escape-sequence-4175712020/)

Faki 05-12-2022 10:33 PM

awk warning on escape sequence
 
I am firing an `awk` command with the character class ere.

Code:

cere='^[[:space:]]*([#;!]+|@c|\/\/)[[:space:]]*'
This gives the awk warning.

Code:

awk: warning: escape sequence `\/' treated as plain `/'
Is this something to worry about or fix?

grail 05-13-2022 12:09 AM

Well with zero clue on how it is being used, it is only a warning, so i guess try it and see?

Faki 05-13-2022 12:17 AM

I want to match C and C++ comments (//)

pan64 05-13-2022 12:54 AM

and your post does not explain how is it used. Anyway, this is a warning, and that warning means [usually] there is a superfluous [or missing] backslash somewhere. Now it was just ignored.

MadeInGermany 05-13-2022 02:11 AM

Try to double each backslash
Code:

cere='^[[:space:]]*([#;!]+|@c|\\/\\/)[[:space:]]*'
Or maybe you just want // then it is
Code:

cere='^[[:space:]]*([#;!]+|@c|//)[[:space:]]*'
The reason is that an RE in an awk variable, when applied in a match operation, is handled in "" (string) context. (In contrast to the // context.)

Faki 05-13-2022 06:22 AM

I want to match regions of lines between a begin line and an end line.
For the following comments in bash, cere will match the comment symbol
followed by any spaces.

Code:

  # Mode: rec
  #  Reads a files and prints out rec format records
  #  Synop: linge-comint-selec SELEC EFILE
  #  Glossary:
  #    SELEC  Selection satisfying some specified criteria.
  #    EFILE  Name of file for searching records.
  # # end of rec

If the code is in C, I would have

Code:

  // Mode: rec
  // Reads a files and prints out rec format records
  // Synop: linge-comint-selec SELEC EFILE
  // Glossary:
  //    SELEC  Selection satisfying some specified criteria.
  //    EFILE  Name of file for searching records.
  // # end of rec

Thus cere would match // followed by any number of spaces.

pan64 05-13-2022 06:39 AM

You still don't understand. Do not only explain what do you want to achieve, but show how did you implement it.
Every command will be first evaluated by the shell before executing it and passing the arguments to it. So the real command line may differ from the one you actually specified.
It all depends on the quotation, escape sequences and other tricky things.
Without details we will unable to se what's going on.

But regarding the original situation: awk reported it found a backslash, but \/ is not a valid escape sequence. TBH it is syntactically incorrect. awk ignored that \ for you - assuming that is just superfluous - and dropped a warning message about that.

Finally why don't you try it, if that works?

Faki 05-13-2022 06:45 AM

Here is the awk call

Code:

    charcl_ere='^[[:space:]]*([#;!]+|@c|\/\/)[[:space:]]*'

    begrec="${charcl_ere}${selec}[[:space:]]*$"
    endrec="${charcl_ere}# end of ${fieldval}[[:space:]]*$"
       
    awk -v ccls="$charcl_ere" -v begrsc="$begrec" -v endrsc="$endrec" \
        '$0 ~ begrsc { insc=1; next }
        $0 ~ endrsc { insc=0; print "" }
        insc { sub(ccls,""); print }' "$efile"


MadeInGermany 05-14-2022 01:16 AM

Then you want // and it should be
Code:

cere='^[[:space:]]*([#;!]+|@c|//)[[:space:]]*'

grail 05-14-2022 08:10 AM

You do realise you can just change the shebang and use all awk?


All times are GMT -5. The time now is 10:51 AM.