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

325
Visualizações
Rgex doen't work with sed command as expected

I have a text file containing :

A 25 27 50

B 35 75

C 75 78

D 99 88 76

I wanted to delete the line that does not have the fourth field(the fourth pair of digits). Expected output :

A 25 27 50

D 99 88 76

I know that awk command would be the best option for such task, but i'm wondering what's the problem with my sed command since it should work as you can see below :

sed -E '/^[ABCD] ([0-9][0-9]) \1$/d' text.txt

Using POSIX ERE with back-referencing (\1) to refer to the previous pattern surrounded with parenthesis.

I have tried this command instead :

sed -E '/^[ABCD] ([0-9][0-9]) [0-9][0-9]$/d' text.txt

But it seems to delete only the first occurrence of what i want. I would appreciate further explanation of,

  • why the back-referencing doesn't work as expected.
  • what's the matter with the first occurrence in the second attempt,should i included global option if yes then how, since i already tried adding it at the end along side with /d (for delete) but it didn't work .
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Much much easier with awk:

awk 'NF == 4' file

A 25 27 50
D 99 88 76

This awk command uses default field separator of space or tab and checks a condition NF == 4 to make sure we print lines with 4 fields only.


With sed it would be (assuming no leading+trailing spaces in each line):

sed -nE '/^[^[:blank:]]+([[:blank:]]+[^[:blank:]]+){3}$/p' file

A 25 27 50
D 99 88 76
over 4 years ago · Santiago Trujillo Relatório

0

With your shown samples in sed program you could try following. Written and tested in GNU sed.

sed -nE '/^([^[:space:]]+[[:space:]]+){3}[^[:space:]]+$/p' Input_file

Explanation: Simply stopping the printing for lines by sed's -n option. Then using -E for using ERE in program. In main program using regex to match from starting non-space(1 or more occurrences) followed by spaces(1 or more occurrences) and this combo 3 times(to match 3 fields basically) which is followed by non spaces 1 or more occurrences till end of line's value, if this regex matched then print that line.

over 4 years ago · Santiago Trujillo Relatório

0

This might work for you (GNU sed):

sed -En 's/\S+/&/4p' file

Turn off implicit printing -n and on extended regexp -E.

Substitute the 4th field with itself and print the result.

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