为了更好地理解大编辑: bash 有可能将我的输出(行)放入csv文件中的列(关于以下挑战)。< / p>
我的剧本:
#!/bin/bash
## get ports, status, vlan
devices=`ls *.txt`
for device in $devices
do
output=`echo $device | awk -F"." {'print $1'}`
echo "Port;Status;VLAN\r" > $output".csv"
port=`cat $device | awk 'BEGIN {OFS=";"}; /Fa[0-9]|Gi[0-9]|Te[0-9]/ && /disabled|connected/ {print $1}'`
stat=`cat $device | awk 'BEGIN {OFS=";"}; /Fa[0-9]|Gi[0-9]|Te[0-9]/ && /disabled|connected/ {print $0}' | awk 'match($0,"connected|disabled.*"){print substr($0,RSTART)}' | awk '{print $1}'`
vlan=`cat $device | awk 'BEGIN {OFS=";"}; /Fa[0-9]|Gi[0-9]|Te[0-9]/ && /disabled|connected/ {print $0}' | awk 'match($0,"connected|disabled.*"){print substr($0,RSTART)}' | awk '{print $2}'`
var=$(paste -d, <(echo "$port") <(echo "$stat") <(echo "$vlan"))
echo "$var" >> output."csv"
done
<input-file.txt>
Port Name Status Vlan Duplex Speed Type
Fa0/1 disabled 1 auto auto 10/100BaseTX
Fa0/2 disabled 1 auto auto 10/100BaseTX
Fa0/3 disabled 1 auto auto 10/100BaseTX
Fa0/4 disabled 1 auto auto 10/100BaseTX
Fa0/5 disabled 1 auto auto 10/100BaseTX
Fa0/6 disabled 1 auto auto 10/100BaseTX
Fa0/7 disabled 1 auto auto 10/100BaseTX
Fa0/8 disabled 1 auto auto 10/100BaseTX
Fa0/9 disabled 1 auto auto 10/100BaseTX
Fa0/10 xxxx-xxx-xxx 0004. connected 3 full 100 10/100BaseTX
Fa0/11 xxxx-xxx-xxx bge0 connected 3 a-full a-100 10/100BaseTX
Fa0/12 xxxx-xxx-xxx iLO connected 2 a-full a-100 10/100BaseTX
Fa0/13 xxxx-xxx-xxx bge0 connected 2 a-full a-100 10/100BaseTX
Fa0/14 disabled 1 auto auto 10/100BaseTX
Fa0/15 xxxx-xxx-vpn bge0 connected 4 a-full a-100 10/100BaseTX
Fa0/16 xxxx-xxx-vpn ilO connected 2 a-full a-100 10/100BaseTX
Fa0/17 xxxx-xxx xx.xx.xx. connected 2 a-full a-100 10/100BaseTX
Fa0/18 xxxxxx-xxx1 connected 2 a-full a-100 10/100BaseTX
Fa0/19 disabled 1 auto auto 10/100BaseTX
Fa0/20 disabled 1 auto auto 10/100BaseTX
Fa0/21 xxxxx-xxxxx disabled 1 auto auto 10/100BaseTX
Fa0/22 xxxxxx-xxx disabled 1 auto auto 10/100BaseTX
Fa0/23 xxxx-xxx disabled 2 auto auto 10/100BaseTX
Fa0/24 xxxxxx-xxxx disabled 1 auto auto 10/100BaseTX
Fa0/25 disabled 1 auto auto 10/100BaseTX
Fa0/26 disabled 1 auto auto 10/100BaseTX
Fa0/27 disabled 1 auto auto 10/100BaseTX
Fa0/28 disabled 1 auto auto 10/100BaseTX
Fa0/29 disabled 1 auto auto 10/100BaseTX
Fa0/30 disabled 1 auto auto 10/100BaseTX
Fa0/31 disabled 1 auto auto 10/100BaseTX
Fa0/32 disabled 1 auto auto 10/100BaseTX
Fa0/33 disabled 1 auto auto 10/100BaseTX
Fa0/34 disabled 1 auto auto 10/100BaseTX
Fa0/35 disabled 1 auto auto 10/100BaseTX
Fa0/36 disabled 1 auto auto 10/100BaseTX
Fa0/37 disabled 1 auto auto 10/100BaseTX
Fa0/38 disabled 1 auto auto 10/100BaseTX
Fa0/39 disabled 1 auto auto 10/100BaseTX
Fa0/40 disabled 1 auto auto 10/100BaseTX
Fa0/41 disabled 1 auto auto 10/100BaseTX
Fa0/42 disabled 1 auto auto 10/100BaseTX
Fa0/43 disabled 1 auto auto 10/100BaseTX
Fa0/44 disabled 1 auto auto 10/100BaseTX
Fa0/45 disabled 1 auto auto 10/100BaseTX
Fa0/46 disabled 1 auto auto 10/100BaseTX
Fa0/47 disabled 1 auto auto 10/100BaseTX
Fa0/48 disabled 1 auto auto 10/100BaseTX
Gi0/1 Xxxxxxxx Xxxxx xx connected trunk a-full a-1000 10/100/1000BaseTX SFP
Gi0/2 Xxxxx xxxxxxx Xxx connected trunk a-full a-1000 1000BaseSX SFP
Gi0/3 disabled 1 auto auto Not Present
Gi0/4 disabled 1 auto auto Not Present
脚本输出的结果应为:
<output.csv>
| Port | Status | VLAN |
| Fa0/1 | disabled | 1 |
| Fa0/2 | disabled | 1 |
| Fa0/3 | disabled | 1 |
| Fa0/4 | disabled | 1 |
| Fa0/5 | disabled | 1 |
| Fa0/6 | disabled | 1 |
| Fa0/7 | disabled | 1 |
| Fa0/8 | disabled | 1 |
| Fa0/9 | disabled | 1 |
| Fa0/10 | connected | 3 |
and so on
问题:在尝试&#34;粘贴&#34;命令(谢谢大家)..我收到以下错误:语法错误:&#34;(&#34;意外
答案 0 :(得分:5)
假设你有GNU awk,循环中的行可以压缩成这样的东西:
awk 'BEGIN{FIELDWIDTHS = "10 18 13 11"}{printf "| %s | %s | %s |\n", $1,$3,$4}' "$device"
如果你有一个不同的awk,命令会更复杂,但仍然可以在一个awk语句中做你想要的。
您尝试的一些要点:
"$details"
而不是$details
),因为这可以保护您免受文件名中空格引起的整类错误。cat
将文件内容传递给awk
。由于大多数程序在其参数中接受文件的名称,因此很少需要使用cat
来处理此类事情。如果他们不这样做,您可以始终使用program < filename
而不是cat filename | program
。ls
是多余的(并且可能容易出现由名称尴尬的文件引起的错误)。使用for device in *.txt
会好得多;做......`最后一点,要警惕陷入困境,考虑一个问题的解决方案,然后询问如何实现它。根据各种工具的经验,其他人可以更容易地为更一般的问题提出最佳解决方案。在这种情况下,最终答案与您原始问题的答案非常不同!我暂时把它留在了下面。
如果您没有GNU awk,请告诉我,我会更新我的答案。
我认为您可能正在寻找paste
命令
paste file.txt anotherf.txt
默认情况下,这两个文件将由制表符分隔。要使用其他内容,您可以指定-d
开关,例如-d,
会将分隔符设置为逗号。
如果您只想要每个文件的第一列,则可以使用带有awk的粘贴:
paste -d, <(awk '{print $1}' file.txt) <(awk '{print $1}' anotherf.txt)
假设两个文件中的列由一个简单的分隔符(如空格)分隔,cut
也会起作用:
paste -d, <(cut -f1 -d' ' file.txt) <(cut -f1 -d' ' anotherf.txt)
根据您问题的更新,如果您已将内容包含在两个变量中,则可以改为使用此命令:
paste -d, <(echo "$var1") <(echo "$var2")
感谢所有参与评论的人。
答案 1 :(得分:1)
您可以使用awk
中的getline
function从单独的文件中读取并同步字段:
$ awk 'getline line<ARGV[2]{split(line,a)}NR==FNR{print $1,a[1]}' file.txt anotherf.txt
1 one
2 two
3 three
4 four
5 five