合并多个awk输出以在一行上打印 [英] Combine multiple awk output to print on one line

查看:139
本文介绍了合并多个awk输出以在一行上打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了两个不同的函数,每个函数都有自己的AWK,可以在多个目录中搜索特定文件并输出所需的信息.这两个函数都会打印FILENAME和我需要的特定字段,并且可以单独正常工作.我想将它们组合在一起以获得更强大的输出.

I have two different functions I wrote, each with their own AWK to search for a specific file in multiple directories and output the information I need. Both functions print the FILENAME and a specific field I need and work just fine on their own. I want to combine them together for more robust output.

我不是程序员.我在阅读有关awk的同时写了这些.

功能1

cver () {
X=""
case $1 in
("-b") X="bb";;
("-c") X="cpe";;
("-e") X="etech";;
("-k") X="core";;
("-o") X="ohgov";;
("-h"|help) echo "'cver' allows us to see the software version 
running on all Company Juniper devices.\n'cver' is especially helpful 
in determining which devices require a software upgrade.\n\nOptions 
are -b for bb, -c for cpe, -e for etech, -k for core, and -o for 
ohgov.\n\nUse grep to find specific information. Ex: cver -c | grep 
name";;
(*) echo "Unrecognized or incomplete command.\nUse -h for help.";;
  esac
[ $X ] && awk -vSRCH="$3" '/JUNOS/ && /boot/ && $5 ~ "^[[]" SRCH  
{sub ("/show.version", "", FILENAME); sub (".*/", "", FILENAME); 
print $5 " " FILENAME}' ~/svn/nw_config_data/*${X}.domain.net/show.version | sort
}

输入/输出示例:

% cver -k   <---input
[11.4R10.3] device.core.domain.net <---ouput

功能2

cmod () {
X=""
case $1 in
("-b") X="bb";;
("-c") X="cpe";;
("-e") X="etech";;
("-k") X="core";;
("-o") X="ohgov";;
esac
awk '/Model/ {sub ("/show.version", "", FILENAME);
sub (".*/", "", FILENAME); print FILENAME " " $2}' 
~/svn/nw_config_data/*${X}.domain.net/show.version | sort
}

输入/输出示例:

% cmod -k <---input
device.core.domain.net mx480 <---output

这些脚本正在执行的操作进入我们的〜/svn/nw_config_data/目录,并为我们部署的每个设备存储一个目录.在每个设备目录中,都有用于配置的文件以及一些show命令.我担心的文件是show.version文件和几行信息.对于"cver",我需要JUNOS Base OS引导信息.对于"cmod",我需要有关Model的信息.然后,"X"将获取不同的设备选项,例如-b ="bb"(device.bb.domain.net)

What these scripts are doing is going into our ~/svn/nw_config_data/ directory and stored there is a directory for every device we have deployed. Within each device directory, there are files for the config, as well as some show commands. The file I'm worried about is the show.version file and a couple lines of information. For "cver" I want the JUNOS Base OS boot information. For "cmod" I want the information for Model. The "X" will then grab different device options like -b="bb" (device.bb.domain.net)

下面是show.version文件的示例.

Below is an example of the show.version file.

Hostname: device-r0
Model: ex3300-24t
JUNOS Base OS boot [12.3R7.7]
JUNOS Base OS Software Suite [12.3R7.7]
JUNOS Kernel Software Suite [12.3R7.7]
JUNOS Crypto Software Suite [12.3R7.7]
JUNOS Online Documentation [12.3R7.7]
JUNOS Enterprise Software Suite [12.3R7.7]
JUNOS Packet Forwarding Engine Enterprise Software Suite [12.3R7.7]
JUNOS Routing Software Suite [12.3R7.7]
JUNOS Web Management [12.3R7.7]
JUNOS FIPS mode utilities [12.3R7.7]

最近几天,我尝试在搜索中查找任何类似且简短的示例.我看到一个回答说用awk表示,但是它只显示第二个awk信息.

I tried searching these last couple days for any kind of examples that would be similar and came up short. I saw one answer that said pipe the awk, but it only prints the second awk info.

然后我尝试了&& awk而不是管道,它使我离得更近,但是在单独的行上打印了第一个awk,然后打印了第二个awk,我希望将它们合并.

I then tried && the awk instead of piping it, and it got me closer, but prints the first awk then the second awk on separate lines and I want them combined.

&&例如:

&& example:

cver () {
X=""
case $1 in
("-b") X="bb";;
("-c") X="cpe";;
("-e") X="etech";;
("-k") X="core";;
("-o") X="ohgov";;
("-h"|help) echo "'cver' allows us to see the software version 
running on all Company Juniper devices.\n'cver' is especially helpful 
in determining which devices require a software upgrade.\n\nOptions 
are -b for bb, -c for cpe, -e for etech, -k for core, and -o for 
ohgov.\n\nUse grep to find specific information. Ex: cver -c | grep 
name";;
(*) echo "Unrecognized or incomplete command.\nUse -h for help.";;
  esac
[ $X ] && awk -vSRCH="$3" '/JUNOS/ && /boot/ && $5 ~ "^[[]" SRCH 
{sub ("/show.version", "", FILENAME); sub (".*/", "", FILENAME); 
print $5 " " FILENAME}' ~/svn/nw_config_data/*${X}.domain.net/show.version 
| sort && awk '/Model/ {sub ("/show.version", "", FILENAME); 
sub (".*/", "", FILENAME); print $2}' 
~/svn/nw_config_data/*${X}.domain.net/show.version | sort

输入/输出示例:

% cver -k <---input
[11.4R10.3] device.core.domain.net <---output
mx480 <---output

我希望输入/输出为:

% cver -k <---input
device.core.domain.net mx480 [11.4R10.3] <---output

我认为命令的打印部分看起来像

Which I imagine the print part of the command to look like

print FILENAME " " $2 " " $5

我也尝试添加/Model/&& $ 2我的第一个功能,但它并没有给我任何的输出,我刚刚失去了在这一点上.

I've also tried adding /Model/ && $2 to my first function, but it doesn't give me any output and I'm just lost at this point.

示例:

cver () {
X=""
case $1 in
("-b") X="bb";;
("-c") X="cpe";;
("-e") X="etech";;
("-k") X="core";;
("-o") X="ohgov";;
("-h"|help) echo "'cver' allows us to see the software version 
running on all Company Juniper devices.\n'cver' is especially helpful 
in determining which devices require a software upgrade.\n\nOptions 
are -b for bb, -c for cpe, -e for etech, -k for core, and -o for 
ohgov.\n\nUse grep to find specific information. Ex: cver -c | grep 
name";;
(*) echo "Unrecognized or incomplete command.\nUse -h for help.";;
  esac
[ $X ] && awk -vSRCH="$3" '/JUNOS/ && /boot/ && $5 && /Model/ && $2 ~ "^[[]" SRCH  
{sub ("/show.version", "", FILENAME); sub (".*/", "", FILENAME); 
print FILENAME " " $2 " " $5}' ~/svn/nw_config_data/*${X}.domain.net/show.version | sort

推荐答案

我刚好向一位同事提到此脚本,发现他有一定的编程经验.他决定对miken32所提供的功能一探究竟,并想出了一个可以满足我所需要的工作脚本.

I just happened to mention this script to a co-worker and found out he has some programming experience. He decided to take a stab at what miken32 provided and came up with a working script that does exactly what I was looking for.

现在,我们计划在不久的将来扩展此脚本,以准确地获取更多信息,因为Juniper并未跨软件版本标准化其show.version信息,也没有从少数几个版本中获取我们所需的信息.由于此脚本基于瞻博网络,因此我们已在现场部署了Cisco设备.

Now, we're planning on expanding this script in the near future to pull more information, accurately, since Juniper doesn't standardized their show.version information across software versions, as well as pulling the information we need from the few Cisco devices we have deployed in the field since this script is Juniper based.

我们添加了一个"-a"选项来拉出所有主机. dirs部分允许我们查看选择了哪个选项(-b,-c,-e等)的所有主机,并将其弹出到for循环中以防止重复信息出现.稍后,我将在旧的cver中添加帮助和(*)选项.

We added a "-a" option to pull ALL hosts. The dirs section allows us to see all hosts for whichever option is chosen (-b,-c,-e, etc), and pops it in a for loop to prevent duplicate information from populating. I'll be adding back the help and (*) options from the old cver later.

我最初的cver脚本的目的只是为了拉动我们所有设备的软件版本,以便我们可以开始计划维护时段以升级Juniper软件.然后我决定也可以看到该模型,因此我编写了cmod脚本来获取模型.我们需要这些信息,因为某些型号在软件升级过程(mx80)中存在错误,需要先抢先重启才能加载软件.

The purpose of my original cver script was just to pull software versions of all our devices so we could start planning maintenance windows to upgrade the Juniper Software. Then I decided it would be nice to see the model too, so I wrote the cmod script to get the model. We needed that information because some models have a bug in the software upgrade process (mx80) that requires a pre-emptive reboot before loading the software.

此脚本非常适合在ISP或什至部署了许多路由器和交换机的企业环境中工作的任何人.现在,您可以快速在这些设备上提取信息,而不必登录到每个设备并手动运行命令来获取此信息.

This script is great for anyone working at an ISP or even an enterprise environment with lots of routers and switches deployed. Now you can quickly pull the information on these devices without having to login to each one and manually running commands to get this information.

现在,新的和改进的cver!

And now, the new and improved cver!

#!/usr/local/bin/zsh
svn="$HOME/svn/nw_config_data/"
 case "$1" in
   ("-a")
   hosts=""
   ;;
   ("-b")
   hosts=".bb.domain.net"
   ;;
   ("-c")
   hosts=".cpe.domain.net"
   ;;
   ("-e")
   hosts=".etech.domain.net"
   ;;
   ("-k")
   hosts=".core.domain.net"
   ;;
   ("-o")
   hosts=".ohgov.domain.net"
   ;;
 esac

dirs=($(ls -d $svn*$hosts*))
for hostdir in $dirs
do
host=$(echo $hostdir | grep -Eo "(\w|\.|-)*$")

awk -v h=$host '/^JUNOS Base OS boot/{v=$5} /^Junos:/{v="["$2"]"} /^Model/{m=$2} END{if(m!="number"){printf("%s %s %s\n",h,m,v)}}' "$hostdir/show.version"
done

这篇关于合并多个awk输出以在一行上打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆