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

205
Visualizações
Linux grep how to treat all lines as a big line for regex?

An example, test.txt:

This is bad, real bad!
<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions targetNamespace="http://tips.cf"
xmlns:impl="http://tips.cf" xmlns:intf="http://tips.cf"
xmlns:apachesoap="http://xml.apache.org/xml-soap"

I have a regex: ^<\?xml.*\?>.

grep match as line by line. So this regex can have a match(the second line).

But I want to make grep treat the lines as a big line, and couldn't have a match, because it is not startswith <?xml.

I tried:

grep -P -z -- '^<\?xml.*\?>' test.txt

use -z but it still match the second line.

Is there a way to use grep to make it unmatch, or there is another regex command line tool?

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

0

If you use \A instead of anchor ^ then it will fail the match:

# finds no match
grep -Pz -- '\A<\?xml.*\?>' file

This grep in a multiline string ^ matches at the start of every line but \A matches at the real start of input.

over 4 years ago · Santiago Trujillo Relatório

0

grep pattern containing newline (bash: $'\n')

Try this:

grep -Pz '\AThis.*\n<\?xml.*\?>' test.txt

, this

grep -Pz '<\?xml.*\?>' test.txt

, this

grep -Pz '^<\?xml.*\?>' test.txt 

and this

grep -Pz '\A<\?xml.*\?>' test.txt 

or this

grep -z $'^This.*\n<\\?xml.*\\?>' test.txt 
over 4 years ago · Santiago Trujillo Relatório

0

You can join the lines to a big line by xargs before applying the regex:

# no match returns
cat test.txt | xargs | grep '^<?xml.*?>'

Check for more usage on xargs

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