Home > Net >  Escape sequence error within awk command after initiating two color variables
Escape sequence error within awk command after initiating two color variables

Time:04-30

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:

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!

CodePudding user response:

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
  • Related