Ricerca…


introduzione

L'operazione di ramificazione di sed può aiutare a controllare il flusso del programma.

Esegui regexp di linee multiple sostituendole con un ramo incondizionato

Supponiamo di avere un file chiamato in.txt :

$ cat in.txt
a
b
a
c
a
d

Voglio solo sostituire a\nc con deleted , ma non a\nb o a\nd .

$ sed -e ':loop         # create a branch/label named `loop`
 $!{
 N                      # append the next line of input into the pattern space
 /\n$/!b loop           # If it is not the last line go to the `loop` branch again
 }
 s/a\nc/"deleted"/' in.txt    # do replacing in the pattern space

a
b
"deleted"    # see! succeed
a
d


Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow