grep data 并将其添加到行尾

grep data 并将其添加到行尾

我有一个像这样的 .dat 文件:

show version
Cisco IOS Software, C2960 Software (C2960-LANBASE-M), Version 12.2(35)SE5, RELEASE SOFTWARE (fc1)
Copyright (c) 1986-2007 by Cisco Systems, Inc.
Compiled Thu 19-Jul-07 20:06 by nachen
Image text-base: 0x00003000, data-base: 0x00D40000

ROM: Bootstrap program is C2960 boot loader
BOOTLDR: C2960 Boot Loader (C2960-HBOOT-M) Version 12.2(25r)SEE6, RELEASE SOFTWARE (fc1)

Electronica_Edif_3 uptime is 17 weeks, 6 days, 0 minutes
System returned to ROM by power-on
System restarted at 13:04:50 gmt Thu Nov 10 2016
System image file is "flash:c2960-lanbase-mz.122-35.SE5/c2960-lanbase-mz.122-35.SE5.bin"

cisco WS-C2960-48TC-L (PowerPC405) processor (revision F0) with 61440K/4088K bytes of memory.
Processor board ID FOC1235Z1W5
Last reset from power-on
2 Virtual Ethernet interfaces
48 FastEthernet interfaces
2 Gigabit Ethernet interfaces
 --More--         The password-recovery mechanism is enabled.

64K bytes of flash-simulated non-volatile configuration memory.
Base ethernet MAC Address       : 00:23:05:46:8B:00
Motherboard assembly number     : 73-9835-09
Power supply part number        : 341-0097-02
Motherboard serial number       : FOC12351HVZ
Power supply serial number      : AZS123206WY
Model revision number           : F0
Motherboard revision number     : A0
Model number                    : WS-C2960-48TC-L
System serial number            : FOC1235Z1W5
Top Assembly Part Number        : 800-26672-04
Top Assembly Revision Number    : A0
Version ID                      : V04
CLEI Code Number                : COM3F00BRC
Hardware Board Revision Number  : 0x02


Switch   Ports  Model              SW Version              SW Image            
------   -----  -----              ----------              ----------          
*    1   50     WS-C2960-48TC-L    12.2(35)SE5             C2960-LANBASE-M     

 --More--         
Configuration register is 0xF

Electronica_Edif_3>

我想从中 grep 系统序列号并将其添加到 .cvs 中某一行的末尾,如下所示:

device_id,ip_address
 BIOTERIO, 148.000.00.189
 N7K-LAN(JAF1651ANDL), 148.000.0.192
 LAB_PESADO, 148.000.000.130
 Arquitectura_Salones, 148.000.000.61
 CIVIL_253, 148.000.000.253
 Arquitectura, 148.000.000.253
 ING_CIVIL, 148.000.000.251
 ING_CIVIL_DIR, 148.000.0.188
 Ingenieria_Posgrado, 148.000.000.253
 Biblio_Barragan, 148.000.000.61
 Electronica_Edif_3, 148.000.000.253

例如,在这种情况下,我想提取 SN 并将其添加到第 12 行(我已经在变量中拥有这个数字),我应该如下所示:

device_id,ip_address
 BIOTERIO, 148.000.00.189
 N7K-LAN(JAF1651ANDL), 148.000.0.192
 LAB_PESADO, 148.000.000.130
 Arquitectura_Salones, 148.000.000.61
 CIVIL_253, 148.000.000.253
 Arquitectura, 148.000.000.253
 ING_CIVIL, 148.000.000.251
 ING_CIVIL_DIR, 148.000.0.188
 Ingenieria_Posgrado, 148.000.000.253
 Biblio_Barragan, 148.000.000.61
 Electronica_Edif_3, 148.000.000.253,FOC1235Z1W5

你可以帮帮我吗?

我知道如何 grep SN 并将其存储到变量中: SN="grep "System serial number" SN_148.228.130.253.dat | sed 's/^.*: //'"

但我不知道如何将其添加到给定的行中。

答案1

这不是一句台词,但这很容易理解。只需将行号和序列号作为变量传递给最终awk调用即可。检查行号是否匹配,如果匹配则添加额外信息!

#!/bin/sh
line=12
sn=$(awk '/System serial number/ {print $5}' SN_148.228.130.253.dat)

awk -v "ln=$line" -v "sn=$sn" '{print (NR==ln) ? $0","sn : $0;}' csvfile

答案2

这应该可以工作并且不需要额外的 grep。序列号由 datfile 提取并添加到 csv 文件中,所有这些都在一个脚本中完成。

#!/bin/bash
line=12
awk -v line=$line 'BEGIN{FS=":"}NR==FNR{if ($1 ~ /System serial number/) \
{sn=$2;nextfile} else {next}}{FS=",";if (FNR==line) $0=$1 FS $2 FS sn}1' datfile cvsfile

请参阅此处的在线测试。

相关内容