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

148
Views
Impresión de líneas entre dos dos patrones

Tengo un archivo como este:

 text1 text2 Pattern1 text3 text4 Pattern1 text5 text2 text6 Pattern1 text7 text8 Pattern2 text2 text9 text10 text2 Pattern1 text11 text12 Pattern1 text13 text14 Pattern1 text15 text16 Pattern2 text1 text2 Pattern1 text3 text4 Pattern1 text5 text6 Pattern1 text7 text2 text8 Pattern2 text21 text22

Quiero imprimir SOLAMENTE

 Pattern1 text7 text8 Pattern2 Pattern1 text15 text16 Pattern2 Pattern1 text7 text2 text8 Pattern2

es decir, si Pattern1 es seguido por Pattern2, entonces imprima líneas entre Pattern1 y Pattern2. No quiero líneas si Pattern1 es seguido por Pattern1.

Un comando awk es muy apreciado.

Estaba intentando como:

 awk '/Pattern1/,/Pattern2' file \> output

pero también da todos los carriles que aparecen entre dos Pattern1 sucesivos.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puedes usar este awk :

 awk ' /^Pattern1$/ {s = $0 ORS; next} s != "" {s = s $0 ORS} /^Pattern2$/ {printf "%s", s; s = ""} ' file Pattern1 text7 text8 Pattern2 Pattern1 text15 text16 Pattern2 Pattern1 text7 text2 text8 Pattern2
over 4 years ago · Santiago Trujillo Report

0

$ awk ' /Pattern1/,/Pattern2/ { # between the first start and end markers if(/Pattern1/) # at the begin marker... b=$0 # ... reset buffer else b=b ORS $0 # or keep on buffering if(/Pattern2/) # at the end marker... print b # ... it is time to output }' file

head -4 de salida:

 Pattern1 text7 text8 Pattern2 ...
over 4 years ago · Santiago Trujillo Report

0

Hacer un montón de suposiciones sobre requisitos no declarados, como si coincidir con el primer o el último Patrón2, qué hacer si falta uno u otro en un archivo, qué hacer si ambos "patrones" aparecen en 1 línea, etc.:

 $ cat tst.awk /Pattern1/ { f=1; rec="" } f { rec = rec $0 ORS if ( /Pattern2/ ) { printf "%s", rec f = 0 } }

 $ awk -f tst.awk file Pattern1 text7 text8 Pattern2 Pattern1 text15 text16 Pattern2 Pattern1 text7 text2 text8 Pattern2

Consulte también ¿Cómo encuentro el texto que coincide con un patrón? .

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!