bash grep awk 忽略字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20273545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grep awk ignore character
提问by Deano
I'm trying to grep the value of "option dhcp-server-identifier" which is 192.168.75.1;
我正在尝试 grep “option dhcp-server-identifier”的值,即 192.168.75.1;
I'm not sure how to ignore the semicolon ";" at the end of IP address.
我不确定如何忽略IP 地址末尾的分号“ ;”。
[root@localhost ~]# cat /var/lib/dhclient/dhclient-eth0.leases
lease {
interface "eth0";
fixed-address 192.168.75.54;
option subnet-mask 255.255.255.0;
option routers 192.168.75.1;
option dhcp-lease-time 4294967295;
option dhcp-message-type 5;
option domain-name-servers 192.168.75.1,8.8.8.8;
option dhcp-server-identifier 192.168.75.1;
option broadcast-address 192.168.75.255;
option host-name "centos-64-x86-64";
option domain-name "cs2cloud.internal";
renew 1 2081/12/15 18:43:55;
rebind 2 2132/12/30 03:09:24;
expire 6 2150/01/03 21:58:02;
}
I have tried the following
我已经尝试了以下
grep dhcp-server-identifier /var/lib/dhclient/dhclient-eth0.leases | awk '{print }'
result is 192.168.75.1;
结果是 192.168.75.1;
Thanks
谢谢
回答by Jotne
Using awk
使用 awk
awk -F" |;" '/dhcp-server-identifier/ {print }' /var/lib/dhclient/dhclient-eth0.leases
192.168.75.1
回答by David W.
Remember that if you're grepping to awk, you can simply use awk. The following are equivalent:
请记住,如果您要使用 awk,则只需使用 awk。以下是等效的:
$ grep dhcp-server-identifier /var/lib/dhclient/dhclient-eth0.leases | awk '{print }'
$ awk '/dhcp-server-identifier/ {print }' /var/lib/dhclients/dchclicent-eth0.leases
Your issue is that the semicolon appears on the end of the name. Instead of simply printing $3
, we can use awk's substr
function to remove that final character. Here's the reference to awk's manpage:
您的问题是分号出现在名称的末尾。$3
我们可以使用 awk 的substr
函数来删除最后一个字符,而不是简单地打印。这是对 awk 联机帮助页的参考:
substr(s, m, n)
the n-character substring of s that begins at position m counted from 1.lengththe length of its argument taken as a string, or of $0 if no argument.
substr(s, m, n) s
的 n 个字符的子串,从 1 开始的位置 m 开始。length其参数的长度作为字符串,如果没有参数,则为 $0。
So, we need the substring of $3
from the first position (1), to the length of $3
minus that last character, so we need to go from the first character to length ($3) - 1
:
所以,我们需要$3
从第一个位置 (1) 到$3
减去最后一个字符的长度的子串,所以我们需要从第一个字符到length ($3) - 1
:
substr(, 1, length() - 1)
That should do it:
那应该这样做:
$ awk '/dhcp-server-identifier/ {print substr(, 1, length() - 1)}' /var/lib/dhclients/dchclicent-eth0.leases
That should do it.
那应该这样做。
回答by hek2mgl
With sed
:
与sed
:
sed -n 's/.*option dhcp-server-identifier \(.*\);//p' file
With grep
and egrep
:
随着grep
和egrep
:
grep 'option dhcp-server-identifier' file | egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'
Output:
输出:
192.168.75.1
回答by Bertrand Caron
What about awk
function substr
:
awk
功能怎么样substr
:
~ $ echo "Hello My Friend!" | awk '{print substr(,0,length()-1)}'
Friend
Note about AWK
关于AWK的注意事项
Awk is a really powerful and complete programming langage. If you find yourself using it more than once every couples of day, you could really benefit in your every day bash working flow by learning more of it (especially the regular expression handling part).
awk 是一种非常强大且完整的编程语言。如果您发现自己每两天使用它不止一次,那么通过学习更多它(尤其是正则表达式处理部分),您真的可以在日常 bash 工作流程中受益。
Source : The AWK Manual
来源:AWK 手册
回答by bbaja42
grep dhcp-server-identifier /var/lib/dhclient/dhclient-eth0.leases | cut -d ' ' -f3 | cut -d';' -f1
Command explanation : Get lines that have ' dhcp-server-identifier'. Split by space, and show me 3rd selection. Split by ';' and show me first selection.
命令说明:获取具有“dhcp-server-identifier”的行。按空格分割,然后显示第三个选项。用';'分割 并显示我的第一个选择。
hek2mgl answer is one that I prefer to use; but for a beginner I usually suggest simpler tools than sed.
hek2mgl 答案是我更喜欢使用的答案;但是对于初学者,我通常建议使用比 sed 更简单的工具。
回答by Hardy Rust
awkversion:
awk版本:
awk -F'[ ;]' '/dhcp-server-identifier/ {print $(NF-1)}' /var/lib/dhclient/dhclient-eth0.leases
Using NF-1 is safer, just in case you have more whitespaces.
使用 NF-1 更安全,以防万一您有更多空格。
perlversion:
perl版本:
perl -ne 'print if s/^.*dhcp-server-identifier +([0-9.]+);$//' /var/lib/dhclient/dhclient-eth0.leases
I think that is the safest 1-liner possible.
我认为这是最安全的 1-liner 可能。
sedversion from hek2mglis good too.
hek2mgl 的sed版本也不错。