使用perl脚本,当我们执行“ ip / addr”时,我想找到具有特定字符串“ link / ether”的以太网适配器的名称。 我想将它们存储在变量中。 像:-
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:21:28:d6:1f:f9 brd ff:ff:ff:ff:ff:ff
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:21:28:d6:1f:fa brd ff:ff:ff:ff:ff:ff
5: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:21:28:d6:1f:fb brd ff:ff:ff:ff:ff:ff
6: ab0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/band 00:21:28:d6:1f:fb brd ff:ff:ff:ff:ff:ff
从上到下应该输出以下内容:-
eth1
eth2
eth3
就像存储在变量中并循环运行。 这是我正在尝试但无法正常工作的地方:-
my $last="";
my @results = `ip addr`;
my $adap="";
my $flag=0;
foreach my $line (@results)
{
if (($line =~ /ether/))
{
$adap = substr($last,3,7);
chomp($adap);
$str = $adap;
$flag=1;
}
}
print $adap;
答案 0 :(得分:0)
尝试一下
my @results =
map { /^ \d+ : \s+ ( eth \d+ ) :/xa; $1 }
grep { /ether/ }
`ip addr`;
print @results;
这将获取`ip addr`
返回的列表,删除不包含醚grep { /ether/ }
的任何行,然后捕获包含eth
的字符串部分,后跟1个或多个数字。
正则表达式末尾的x
允许嵌入空格,并且限制\d
只能为ASCII数字。
当您重新打印列表时,不需要剪切行,因为您希望行的结尾。
答案 1 :(得分:0)
它可以像下面一样工作,只将输出存储在@array中:-
@Query("select * from user where remove = false", nativeQuery = true)
答案 2 :(得分:0)
好的。我想这就是您想要的。
Console 0:
>>> f=open('test', 'w+')
>>> f.write("line 1")
>>> f.close()
Console 1:
>>> f=open('test', 'a')
>>> f.write("I opened and wrote first\n")
Console 2:
>>> f=open('test', 'a')
>>> f.write("I closed first\n")
>>> f.close()
Console 1:
>>> f.close()
Console 0:
>>> f=open('test', 'r')
>>> f.readlines()
['line 1\n', 'I closed first\n', 'I opened and wrote first\n']
为将来提供参考,如果您向我们提供展示您所要求的行为的实际代码,这将非常有帮助。