Hi there,
I need to grep out 1 line of a changing file. Any help would be much appreciated.
code:
xterm -hold -e tail -f /var/lib/dhcp3/dhcpd.leases | grep client-hostname &>/dev/null &
The trouble is it shows the contents of the entire lease file.
I just want to show the line starting with client-hostname or even better just the host name so in the case below it just shows computer01
lease 10.0.0.11 {
starts 4 2012/04/12 16:25:30;
ends 4 2012/04/12 16:35:30;
cltt 4 2012/04/12 16:25:30;
binding state active;
next binding state free;
hardware ethernet XX:XX:XX:XX:XX:XX;
uid "\001\000\034\263|>\330";
client-hostname "Computer01";
}
Thank you in advance
DV
The | isn't run by xterm, it happens in whatever shell you're running xterm in. So it does nothing to the contents that get printed to the xterm.
You can try this:
xterm -hold -e sh -c 'tail -f /var/lib/dhcp3/dhcpd.leases | grep client-hostname' &>/dev/null &
The sh -c tells it to run the contents of the string after it inside a shell, which you need to understand the |.
And since the | is in a string, it happens inside xterm like you want.
Is there anyway i could get grep to just show the connected device and not the whole line? So it would just show Computer01 Thanks Dan
grep isn't a programming language, its options for what parts of the match get printed are limited.
How about awk? It is a programming language and understands tokens. Here the program is just 'for all lines that match the regex /client-hostname/, print column two'.
xterm -hold -e sh -c "tail -f /var/lib/dhcp3/dhcpd.leases | awk '/client-hostname/ { print $2 }'" &>/dev/null &
It still displays: client-hostname "Computer01"; and not just: Computer01 Thanks for all your help.
xterm -hold -e sh -c "tail -f /var/lib/dhcp3/dhcpd.leases | awk '/client-hostname/ { gsub(/\"/, \"\"); print $2 }'" &>/dev/null &