Home > OS >  How to ignore # in awk script
How to ignore # in awk script

Time:03-10

I have an awk script that turns a config file into a csv. I am trying to ignore lines that start with #. Would I have to use \#?

This is my script

#!/bin/awk -f

# the line doesn't container "#" or "]" or "[" go here
$0 ~ /(#|\[|\])/ {
    # $1 $2        $3                            $4 $5 
    # 017 : Maximum_Plug_Speed                    = 75
    
    # config_name, option_number, option_value, NULL, NULL, NULL
    printf "%s\b\b\b\b,%s,%s,,,", FILENAME, $1, $5
}

# the line doesn't contain # and contains "[" or "]" got here
$0 ~ /[^#]/ && /([|])/ {
    # $1 $2        $3                            $4 $5             $6  $7 $8  $9
    # 019 * Overspeed_Limit_Set_Point             = 73              R [50 73] MPH

    # config_name, option_number, option_value, option_range_def, option_range_low, option_range_high  
    printf "%s\b\b\b\b,%s,%s,%s%s%s%s,$s,$s", FILENAME, $1, $5, $6,  $7, $8, $9, substr($7,1), $8
}

Is this right?

CodePudding user response:

You can easily skip lines that start with '#' or have their first non-whitespace character as '#' by writing a rule to skip to the next line. You can include as your first rule:

/^ *#/ {next}

Which will do what you need.

Explanation of the REGEX

  • /^ *#/ - match at ^ the beginning of the line zero or more whitespace characters * (space*) followed by a '#'

Example Use/Output

$ awk '/^ *#/ {next}1' << eof
> # my dog
> has fleas
>    # my cat
> has none
> eof
has fleas
has none

(the 1 in the example above is just shorthand for the default print operation)


Skipping Additional Characters

If you really do want to skip all lines that contain '#' or '[' or ']', (as shown as the first comment in your script) then you can add (ex1|exp2|exp3) where expressions exp1..3 are optionally matched regular expressions. You will need to escape \[ and \] as both '[' and ']' have special meaning as part of a character class, e.g.

/(^ *#|\[|\])/ {next}

That will skip lines with '#' as the first non-whitespace character or '[' or ']' anywhere in the line.

Example

$ awk '/(^ *#|\[|\])/ {next}1' << eof
> # my dog
> has [fleas
>    # my cat
> ]has none
> lucky cat!
> eof
lucky cat!

CodePudding user response:

If you want to check if line does contain certain character and do not want to care about escpaing you might exploit index function, which is one of String Functions. It does return position of said character if it was found else 0, thus if you use that as condition it would be met if character was found anywhere.

For example printing all lines which contain [ might be done via

awk 'index($0,"["){print}' file.txt
  • Related