В одну сторону, используя sed
:
sed "s/^$var1/$var2/ ; ta ; b ; :a ; N ; ba" infile
Пояснение:
s/^$var1/$var2/ # Do substitution.
ta # If substitution succeed, go to label `:a`
b # Substitution failed. I still haven't found first line to
# change, so read next line and try again.
:a # Label 'a'
N # At this position, the substitution has been made, so begin loop
# where I will read every line and print until end of file.
ba # Go to label 'a' and repeat the loop until end of file.
Тест с тем же примером, предоставленным Jaypal:
Содержимое infile
:
ab aa
ab ff
baba aa
ab fff
Запустите команду:
sed "s/^$var1/$var2/ ; ta ; b ; :a ; N ; ba" infile
И результат:
bb aa
ab ff
baba aa
ab fff