Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
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

  • Start with #
  • Any number of white space then a #
  • blank lines
  • Remove (and including) # and everything after it if line starts with a string.

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?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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
over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

Here is another working solution by using sed:

config=$(sed -r -e '/^$/d' -e '/^ *#/d' -e 's/^(.+)(#.*)$/\1/' [[YOUR_CONFIG_FILE_NAME]])
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda