LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-12-2022, 10:33 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 185

Rep: Reputation: Disabled
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?
 
Old 05-13-2022, 12:09 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,892

Rep: Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122
Well with zero clue on how it is being used, it is only a warning, so i guess try it and see?
 
Old 05-13-2022, 12:17 AM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 185

Original Poster
Rep: Reputation: Disabled
I want to match C and C++ comments (//)
 
Old 05-13-2022, 12:54 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 18,444

Rep: Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263
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.
 
Old 05-13-2022, 02:11 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,983

Rep: Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890
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.)

Last edited by MadeInGermany; 05-13-2022 at 02:28 AM.
 
Old 05-13-2022, 06:22 AM   #6
Faki
Member
 
Registered: Oct 2021
Posts: 185

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-13-2022, 06:39 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 18,444

Rep: Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263Reputation: 6263
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?
 
Old 05-13-2022, 06:45 AM   #8
Faki
Member
 
Registered: Oct 2021
Posts: 185

Original Poster
Rep: Reputation: Disabled
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"
 
Old 05-14-2022, 01:16 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,983

Rep: Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890
Then you want // and it should be
Code:
cere='^[[:space:]]*([#;!]+|@c|//)[[:space:]]*'
 
Old 05-14-2022, 08:10 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,892

Rep: Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122Reputation: 3122
You do realise you can just change the shebang and use all awk?
 
  


Reply

Tags
awk, awk regex


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell: how to escape (or not escape) $ within an echo statement? Paul_N Linux - Newbie 2 04-05-2016 01:59 AM
unknown escape sequence: '\040' jiikka Linux - General 1 03-09-2009 07:48 AM
escape sequence help in C name_in_use450 Linux - General 6 07-01-2004 09:23 AM
escape sequence for changing color of prompt killer_bunny Linux - General 1 02-10-2004 07:40 PM
smiley face escape sequence problem mandrakeroot Programming 12 09-06-2003 06:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration