如何在SED中使用AWK

时间:2017-11-28 11:01:59

标签: linux bash awk sed

我正在尝试从文件中的字符串中删除一些单词。问题是文件中出现了这个词。

我需要从文件中删除主机名,所以我在做:

   less /etc/dhcp/dhclient.conf | grep host-name 

这就是我得到的:

send host-name = gethostname();
    domain-name, domain-name-servers, domain-search, host-name,
#  option host-name "andare.swiftmedia.com";

所以现在我尝试使用awk:

less /etc/dhcp/dhclient.conf | grep host-name | awk 'NR>1 {print $4}'
host-name,
"andare.swiftmedia.com";

但我不知道如何从特定事件(第二次出现)的文件中删除它

sed和awk总是让我感到困惑:/ 非常感谢您的帮助。

修改 当前/etc/dhcp/dhclient.conf

# Configuration file for /sbin/dhclient.
#
# This is a sample configuration file for dhclient. See dhclient.conf's
#       man page for more information about the syntax of this file
#       and a more comprehensive list of the parameters understood by
#       dhclient.
#
# Normally, if the DHCP server provides reasonable information and does
#       not leave anything out (like the domain name, for example), then
#       few changes must be made to this file, if any.
#

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;

send host-name = gethostname();
request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, domain-search, host-name,
        dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,
        netbios-name-servers, netbios-scope, interface-mtu,
        rfc3442-classless-static-routes, ntp-servers;

我需要从请求开始删除主机名。

预期:

# Configuration file for /sbin/dhclient.
#
# This is a sample configuration file for dhclient. See dhclient.conf's
#       man page for more information about the syntax of this file
#       and a more comprehensive list of the parameters understood by
#       dhclient.
#
# Normally, if the DHCP server provides reasonable information and does
#       not leave anything out (like the domain name, for example), then
#       few changes must be made to this file, if any.
#

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;

send host-name = gethostname();
request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, domain-search,
        dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,
        netbios-name-servers, netbios-scope, interface-mtu,
        rfc3442-classless-static-routes, ntp-servers;

此致

1 个答案:

答案 0 :(得分:2)

如果您只想在特定示例中删除“host-name”的第二个外观,可以通过删除字符串“host-name”来简化它,即:

sed -e 's/ host-name,//g'

因此它不会影响与“host-name”完全匹配的字符串