Szukaj…


Wprowadzenie

Rozgałęziająca operacja sed może pomóc kontrolować przebieg programu.

Wykonaj wielokrotne wyrażenie regularne zastępując gałąź bezwarunkową

Załóżmy, że mam plik o nazwie in.txt :

$ cat in.txt
a
b
a
c
a
d

Chcę tylko zamienić a\nc deleted , ale nie a\nb ani 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
Licencjonowany na podstawie CC BY-SA 3.0
Nie związany z Stack Overflow