Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
¿Linux grep cómo tratar todas las líneas como una línea grande para expresiones regulares?

Un ejemplo, 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"

Tengo una expresión regular: ^<\?xml.*\?> .

grep coincide como línea por línea. Entonces esta expresión regular puede tener una coincidencia (la segunda línea).

Pero quiero que grep trate las líneas como una línea grande, y no podría tener una coincidencia, porque no comienza con <?xml .

Lo intenté:

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

use -z pero aún coincide con la segunda línea.

¿Hay alguna manera de usar grep para que no coincida, o hay otra herramienta de línea de comandos de expresiones regulares?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Si usa \A en lugar de ancla ^ , fallará la coincidencia:

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

Este grep en una cadena multilínea ^ coincide al comienzo de cada línea, pero \A coincide con el comienzo real de la entrada.

over 4 years ago · Santiago Trujillo Report

0

patrón grep que contiene nueva línea ( bash : $'\n' )

Prueba esto:

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

, este

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

, este

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

y esto

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

o esto

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

0

Puede unir las líneas a una línea grande mediante xargs antes de aplicar la expresión regular:

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

Compruebe si hay más uso en xargs

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!