【问题标题】:bash output to csv columnsbash 输出到 csv 列
【发布时间】:2014-09-11 13:17:39
【问题描述】:

大编辑以便更好地理解: bash 中是否有可能将我的输出(行)放入 csv 文件中的列(关于以下挑战)。

我的脚本:

#!/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

问题:尝试“粘贴”命令后(谢谢大家).. 我收到以下错误:语法错误:“(”意外

【问题讨论】:

  • 如果您在问题中包含.txt 文件之一的简短示例,将会很有用。
  • 将输入数据的简短样本放在问题中并显示相应的所需输出会更有用。您还可以从问题中删除各个 awk 命令的输出。
  • 我已经更新了我的答案。
  • 感谢您的提示,之前从未使用过 FIELDWIDTHS,但提到的输入文件仅显示了整个配置文件内容的 1/10。因为不同查询的输出似乎具有相同的结构。因此,即使在扩展固件查询之后,“10 19 13 11”也有更多匹配项 - 是的,我正在使用 gnu awk 4
  • 我做了一个解决方法来解决我的“语法”问题......通过将我的查询输出写入文件然后执行“paste f1 f2 f3”......完美。解决了!

标签: bash csv


【解决方案1】:

假设你有 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 &lt; filename 而不是 cat filename | program。
  • ls 的使用是多余的(并且可能容易出现由名称难看的文件引起的错误)。使用for device in *.txt会好很多;做...`

最后一点,请小心不要陷入思考问题的一种解决方案,然后询问如何实施它。根据他们对各种工具的经验,其他人更容易为更普遍的问题提出最佳解决方案。在这种情况下,最终答案与您原始问题的答案非常不同!我暂时把它放在下面了。

如果您没有 GNU awk,请告诉我,我会更新我的答案。


我认为您可能正在寻找paste 命令

paste file.txt anotherf.txt

默认情况下,这两个文件将由制表符分隔。要使用其他内容,您可以指定-d 开关,例如-d, 会将分隔符设置为逗号。

如果您只想要每个文件的第一列,您可以将 paste 与 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")

感谢所有为 cmets 做出贡献的人。

【讨论】:

  • 可能是paste &lt;(awk '{print $1}' f1) &lt;(awk '{print $1}' f2)
  • 或 ;) paste -d, &lt;(cut -f1 -d' ' f1) &lt;(cut -f1 -d' ' f2)
  • 根据 OP 的新要求,paste &lt;(echo "$var1") &lt;(echo "$var2") 应该做。顺便说一句,我已经 +1,但再次获得虚拟 +1 以耐心改进答案!
  • 谢谢!但仍然遇到问题:语法错误:“(”意外
  • @jayFour 也许展示问题中变量包含的样本会有所帮助。另外,尝试使用set -x 启用调试。
【解决方案2】:

您可以使用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

【讨论】:

  • 确实有一些有用的材料,谢谢。看完后,我觉得我对getline的用法似乎是合适的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多