2013-04-25 15 views
5

Chciałbym dodać nową linię po dopasowaniu ciągu + 2 linie."sed": Jak dodać nową linię po dopasowaniu ciągu + 2 linie

Oto mój plik:

allow-hotplug eth0 
auto eth0 
iface eth0 inet static 
address 172.16.2.245 
netmask 255.255.254.0 
gateway 192.168.1.1 

allow-hotplug eth1 
#auto eth1 
iface eth1 inet static 
address 192.168.0.240 
netmask 255.255.255.0 

iface eth2 inet static 
address 192.168.2.240 
netmask 255.255.255.0 

chcę dodać 'bramy 192.168.1.1' po odnaleziono IFACE eth1 '+ 2 linie.

przykład: co muszę dostać po sed wykonać polecenie

allow-hotplug eth0 
auto eth0 
iface eth0 inet static 
address 172.16.2.245 
netmask 255.255.254.0 
gateway 172.16.2.254 

allow-hotplug eth1 
#auto eth1 
iface eth1 inet static 
address 192.168.0.240 
netmask 255.255.255.0 
gateway 192.168.1.1 

iface eth2 inet static 
address 192.168.2.240 
netmask 255.255.255.0 

wiem, jak znaleźć i przenieść po 2 linie, dołącz wiersz po konkretnym ciągiem itp ale nie łączą tę operację 2. Steph

Odpowiedz

0

jeśli chcesz dodać wiersz na końcu tego bloku, spróbuj tego:

awk -v RS="" -v ORS="\n\n" '/iface eth1/{$0=$0"\ngateway 192.168.1.1"}1' file 

z plikiem:

kent$ cat file 
allow-hotplug eth0 
auto eth0 
iface eth0 inet static 
address 172.16.2.245 
netmask 255.255.254.0 
gateway 192.168.1.1 

allow-hotplug eth1 
#auto eth1 
iface eth1 inet static 
address 192.168.0.240 
netmask 255.255.255.0 

iface eth2 inet static 
address 192.168.2.240 
netmask 255.255.255.0 

kent$ awk -v RS="" -v ORS="\n\n" '/iface eth1/{$0=$0"\ngateway 192.168.1.1"}1' file 
allow-hotplug eth0 
auto eth0 
iface eth0 inet static 
address 172.16.2.245 
netmask 255.255.254.0 
gateway 192.168.1.1 

allow-hotplug eth1 
#auto eth1 
iface eth1 inet static 
address 192.168.0.240 
netmask 255.255.255.0 
gateway 192.168.1.1 

iface eth2 inet static 
address 192.168.2.240 
netmask 255.255.255.0 
6

To wydaje się działać:

sed '/^iface eth1/{N;N;s/$/\ngateway 192.168.1.1/}' input.txt 

Dodaj opcję -i do sed, aby zapisać wynik ponownie do input.txt.

+0

Wielkie dzięki! to jest naprawdę pomocne. – user2319609

+0

@ user2319609 Cieszę się, że mogłem pomóc. Możesz przy okazji [przyjąć odpowiedź] (http://meta.stackexchange.com/a/5235/181223). –

+0

Fantastyczna odpowiedź. Użyję tego często, ponieważ robiłem coś w stylu 'line_num2 = $ (grep -n 'txt_to_find' targ_file.h | awk '{print $ 1}')', a następnie dodawaj, aby uzyskać numer wiersza, którego potrzebowałem! Jest to o wiele bardziej wydajne. – Zmart

2

Jednym ze sposobów, za pomocą sed:

sed ' 
/iface eth1/ { 
n 
n 
a\gateway 192.168.1.1 
}' file 
0

Pytałeś używać „sed” ale „Kent” używa 'awk, sed o to skrypt, który robi to, co chcesz dla przykładu. Aby być bardziej ogólnym, wiersz 1 skryptu sed może zawierać dowolny ciąg znaków, a wiersz 5 skryptu sed może zawierać dowolny ciąg znaków. Umieść następujący skrypt w pliku, na przykład x.sed, nie dodawaj spacji ani kart.

/iface eth1/{ 
    n 
    n 
    a\ 
    gateway 192.168.1.1 
    } 

Następnie uruchom go w ten sposób w linii poleceń.

sed -f x.sed "myinputfile" > "myoutputfile" 
+0

Dziękuję, próbowałem zrobić to samo, jak to ... sed -i.bak '/^iface eth1/{n; n; a \ gateway 192.168.1.1}'/etc/network/interfaces – user2319609

+0

... bez powodzenie !!?? Nie wiem dlaczego? – user2319609

Powiązane problemy