Dave
Dave

Reputation: 1241

Bash match on any lines that have any number of whitespace up to #

I need to clean up a config file before a source it. I need to remove any lines that have

Example config:

# comment
     # comment
dog=woof
cat=meow
moose=huuuuu #comment

# comment
### comment

I have this right now

config_params="$(cat ./config_file.conf | grep -v "^#.* | awk -F '=' '{print$1}')"

The problem is line 2, # comment any number of space up to a #. How can I match to remove lines like this?

Upvotes: 1

Views: 472

Answers (4)

Paul Hodges
Paul Hodges

Reputation: 15273

I'd use sed.

$ cat file
# comment
     # comment
dog=woof
cat=meow
moose=huuuuu #comment

# comment
### comment

$ sed -E 's/ *#.*//; /^$/d;' file
dog=woof
cat=meow
moose=huuuuu

$ temp=$(mktemp)
$ sed -E 's/ *#.*//; /^$/d;' file>$temp
$ . $temp
$ echo $moose
huuuuu

Also, UUoC is an antipattern, and chaining regex tools kind of is too.
And why are you saving just the keys to a variable without values?
(I added a closing paren & quote.)

If that's really what you wanted, then instead of

config_params="$(cat ./config_file.conf | grep -v "^#.* | awk -F '=' '{print$1}')"

try

config_params="$(awk -F '=' '/^ *(#.*)*$/{next} {print $1}' ./config_file.conf)"

Though I thought the point was to source them...

Upvotes: 0

Antonio Petricca
Antonio Petricca

Reputation: 11016

Here is another working solution by using sed:

config=$(sed -r -e '/^$/d' -e '/^ *#/d' -e 's/^(.+)(#.*)$/\1/' [[YOUR_CONFIG_FILE_NAME]])

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

You can use

config_params=$(awk -F'=' '!/^[[:space:]]*#/{print $1}' ./config_file.conf)

See the online demo:

#!/bin/bash
s='# comment
     # comment
dog=woof
cat=meow
moose=huuuuu #comment
 
# comment
### comment'
awk -F'=' '!/^[[:space:]]*#/{print $1}' <<< "$s"

Output:

dog
cat
moose

Here, ^[[:space:]]*# matches start of a string, then zero or more whitespaces, and then a #. The ! negates the regex match result, so only the lines that do not match this pattern are "taken", and then their Field 1 values are only printed.

Upvotes: 1

anubhava
anubhava

Reputation: 785058

You may use this awk:

awk -F= 'NF == 2 {sub(/[[:blank:]]*#.*/, ""); print}' file

dog=woof
cat=meow
moose=huuuuu

Or if you want to print only key names then use:

awk -F= 'NF == 2 {sub(/[[:blank:]]*#.*/, ""); print $1}' file

dog
cat
moose

Upvotes: 2

Related Questions