Domanda

Non ho più file dove sono l'estrazione di testo tra due delimitatori (inizio e fine non-inclusive). Dopo che ho elaborare ogni file che voglio uscita un delimitatore che indica il file è stato elaborato, dire la stringa "FINE". Come vorrei aggiungere la "fine" di stringa all'uscita dopo ogni file elaborato? Ad esempio,

Di 'Ho due file con il contenuto

file1.txt

line 1
line 2
START
I want to extract this text
and this text
END
line 3
line 4

e

file2.txt

line10
line20
START
I also want this text.
END
line 30

Lo so se eseguo il seguente:

awk '/START/,/END/{if (!/START/&&!/END/) print}' test*.txt

I otterrà il seguente output:

I want to extract this text
and this text
I also want this text.

Ma l'uscita che voglio è:

I want to extract this text
and this text
END
I also want this text.
END

Come faccio ad avere la stringa "FINE", ha aggiunto all'uscita dopo ogni file viene elaborato?

È stato utile?

Soluzione

FNR==1 && NR!=1 {print "END"}
END {print "END"}

questo modo ci sono:

awk 'FNR==1 && NR!=1 {print "END"} /START/,/END/{if (!/START/&&!/END/) print} END {print "END"}' test*.txt

Altri suggerimenti

la variabile FNR è resettato a 0 su ogni nuovo file, quindi si potrebbe avere una regola grilletto (FNR == 0 && NR = 0!):

(FNR==0) && (NR!=0) {
  print "END"
}
END {
  print "END"
}

Stai chiedendo awk per saltare FINE stampa. Smettila.

awk '/START/,/END/{if (!/START/) print}' test*.txt
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top