仅从文件中提取主机名

问题描述 投票:0回答:1

我试图仅提取该主机名中的主机名和命令状态,但无法找到有用的 grep 选项来执行此操作。文件格式如下

server1


bash: adinfo: command not found

我正在尝试以一种只给我主机名和 adinfo 命令的命令输出的方式使用 grep/sed。所以为此我已经尝试过

more file3 |grep 'com.*nd'

bash: adinfo: command not found
bash: adinfo: command not found

所以我得到了部分输出,但没有得到上面列出的服务器名称。理想情况下,它应该列出服务器名称和该命令输出。

任何帮助表示赞赏。

more file3 |grep 'com.*nd'

澄清一下,我有一个包含以下内容的大文件:

server1

Local host name:   server1
Joined to domain:  domain.com
Joined as:         server1.domain.com
Pre-win2K name:    server1
Current DC:        domain.com
Preferred site:    Datacenter
Zone:              servers Zone
Last password set: 2022-03-11 02:23:55 EST
CentrifyDC mode:   connected
Licensed Features: Enabled

server2

bash: adinfo: command not found 

我只想查看无法运行“adinfo”命令的位置以及服务器名称。我可以缩小到

# more file3 |grep -e 'Loc.*me' -e 'Cen.*de'
Local host name:   server1
CentrifyDC mode:   connected

但反过来我只能看到广告信息不起作用的服务器。

linux awk grep redhat
1个回答
0
投票

使用这个 Perl 单行代码:

perl -ne 'chomp; next LINE if !m{\S}; if ( m{bash:\s+adinfo:\s+(.*)} ) { print "$1\n" } else { print "$_\t" } ' infile > outfile

Perl 单行代码使用这些命令行标志:

-e
:告诉 Perl 查找内联代码,而不是在文件中。
-n
:一次循环输入一行,默认情况下将其分配给
$_

chomp;
: 从当前输入行中删除作为输入记录分隔符的尾随字符串,例如 *NIX 中的换行符。
\s+
: 1个或多个空白字符。
(.*)
:0个或多个任意字符。括号捕获匹配的字符串并将其存储在变量 $1 中。
$_
: 当前输入线。

另见:

perldoc perlrun
:如何执行Perl解释器:命令行开关
perldoc perlre
:Perl正则表达式(regexes)

perldoc perlrequick
:Perl正则表达式快速入门

© www.soinside.com 2019 - 2024. All rights reserved.