До совпавшей строки:
awk -v line='127.0.1.1 cent.centurian.com centurian' '/127\.0\.0\.1/ \
{ printf "%s\n%s\n", line, $0; next }; 1' file.txt
После совпавшей строки:
awk -v line='127.0.1.1 cent.centurian.com centurian' '/127\.0\.0\.1/ \
{ printf "%s\n%s\n", $0, line; next }; 1' file.txt
/127\.0\.0\. 1/
совпадает с шаблоном
Если шаблон совпал, то нужный форматированный вывод печатается с помощью printf
на основе того, находится ли переменная line
до или после совпавшей строки
Пример:
$ cat file.txt
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
$ awk -v line='127.0.1.1 cent.centurian.com centurian' '/127\.0\.0\.1/ { printf "%s\n%s\n", $0, line; next }; 1' file.txt
127.0.0.1 localhost
127.0.1.1 cent.centurian.com centurian
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
$ awk -v line='127.0.1.1 cent.centurian.com centurian' '/127\.0\.0\.1/ { printf "%s\n%s\n", line, $0; next }; 1' file.txt
127.0.1.1 cent.centurian.com centurian
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters