Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Escape sequence error within awk command after initiating two color variables

I want to iterate over my file and all with awk which works fine, but when tried to insert my COLOR and WHITE variables I realized that I would have to first initialize it within the awk command like so: -v COLOR="${COLOR}" and WHITE="${WHITE}". Yet when I did so I started getting the following error:

awk: warning: escape sequence `\e' treated as plain `e'
awk: cmd. line:1: WHITE=\e[1;37m
awk: cmd. line:1:       ^ backslash not last character on line
awk: cmd. line:1: WHITE=\e[1;37m
awk: cmd. line:1:       ^ syntax error

Full Code:

WHITE="\e[1;37m"
COLOR="\e[1;31m"

awk -v COLOR="${COLOR}" WHITE="${WHITE}"  '{system("sleep 0.1");print "    (COLOR" NR "WHITE) " $0 }' < settings.tropx

the settings.tropx file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

some setting
some other setting
set ting
other setting

Please Explain what the reason for this could be and how I can fix it, Thank You!

>Solution :

Would you please try:

#!/bin/bash

WHITE=$'\e[1;37m'
COLOR=$'\e[1;31m'

awk -v COLOR="$COLOR" -v WHITE="$WHITE" '
    {
        system("sleep 0.1")
        print "    ("COLOR NR WHITE") " $0
    }
' settings.tropx

We need to use ANSI quoting $'..' with bash to include an escape sequence. But if you do not have a specific reason to use -v mechanism, you can also say:

awk '
    BEGIN {COLOR="\033[1;31m"; WHITE="\033[1;37m"}
    {
        system("sleep 0.1")
        print "    ("COLOR NR WHITE") " $0
    }
' settings.tropx
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading