Найти и заменить в выводе команды на основе содержимого файла

295
o.comp

У меня есть tail -fкоманда чтения журнала, содержащего IP-адреса, однако я хочу заменить эти IP-адреса именами хостов, у меня есть dhcpd.leasesфайл, доступный для компьютера, на котором он выполняется.

Я был в состоянии преобразовать dhcp.leasesфайл в (возможно?) Более удобный формат:

10.0.0.1 Hostname1 10.0.0.2 Hostname2 
1

1 ответ на вопрос

1
chaos

Assuming that the filename of the file you want to tail is file and the filename of the list is list. I also assume that the file list looks like:

10.0.0.1 Hostname1 10.0.0.2 Hostname2 

Then use this:

tail -f file | while read l; do \ while read i h; do l="$"; done <list; echo "$l"; \ done 
  • The tail command is piped to a while loop that reads the input line by line into the variable $l.
  • Inside that while loop, there is another while loop that reads the file list line by line and replaces the values from list in the $l variable.
  • Then the line is printed to stdout.

Похожие вопросы