linux - 在 Linux shell 脚本中解析

标签 linux shell parsing

我正在尝试解析以下输出:

+--------------------------------------+-----------------+-----------+------+-----------+------+-------+-------------+-----------+
| ID                                   | Name            | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |
+--------------------------------------+-----------------+-----------+------+-----------+------+-------+-------------+-----------+
| 1                                    | m1.tiny         | 512       | 1    | 0         |      | 1     | 1.0         | True      |
| 2                                    | m1.small        | 2048      | 20   | 0         |      | 1     | 1.0         | True      |
| 214b272c-e6a4-4bb5-96a4-c74c64984e5a | MC              | 2048      | 100  | 0         |      | 1     | 1.0         | True      |
| 3                                    | m1.medium       | 4096      | 40   | 0         |      | 2     | 1.0         | True      |
| 4                                    | m1.large        | 8192      | 80   | 0         |      | 4     | 1.0         | True      |
| 5                                    | m1.xlarge       | 16384     | 160  | 0         |      | 8     | 1.0         | True      |
| 71aa57d1-52e3-4499-abd2-23985949aeb4 | slmc            | 4096      | 32   | 0         |      | 2     | 1.0         | True      |
| 7cf1d926-c904-47b8-af70-499196a1f65f | new test flavor | 1         | 1    | 0         |      | 1     | 1.0         | True      |
| 97b3dc38-f752-437b-881d-c3415c8a682c | slstore         | 10240     | 32   | 0         |      | 4     | 1.0         | True      |
+--------------------------------------+-----------------+-----------+------+-----------+------+-------+-------------+-----------+

它是open-stack中的flavor列表。我期待如下输出:

m1.tiny;m1.small;MC;m1.medium;m1.large;m1.xlarge;slmc;new test flavor;slstore;

我尝试了什么:

我想出了以下解析命令:

nova flavor-list | grep '|' | awk 'NR>1 {print $4}' | tr '\n' ';'

但问题是命令返回输出如下:

m1.tiny;m1.small;MC;m1.medium;m1.large;m1.xlarge;slmc;new;slstore;

new test flavor 中的空间有问题。

最佳答案

下面的命令将给出预期的输出

nova flavor-list | grep '|' | awk -F "|" 'NR>1 {print $3}' | tr '\n' ';'

以上命令将输出空格,即

$ nova flavor-list | grep '|' | awk -F "|" 'NR>1  {print $3}' | tr '\n' ';'
 m1.tiny         ; m1.small        ; MC              ; m1.medium       ; m1.large        ; m1.xlarge       ; slmc            ; new test flavor ; slstore         ;

要获得没有空格的输出,请使用以下命令

$nova flavor-list | grep '|' | awk -F "|" 'NR>1  {print $3}' | awk -F "\n" '{gsub(/^[ \t]+|[ \t]+$/, "", $1)}1' | tr '\n' ';' 
m1.tiny;m1.small;MC;m1.medium;m1.large;m1.xlarge;slmc;new test flavor;slstore;

关于linux - 在 Linux shell 脚本中解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38144162/

相关文章:

linux - 如何对特定字段进行 grep 而不是整行

c - C 中的父/子和管道,子-父通信

shell - 具有不同文件扩展名的ffmpeg图像concat文件

linux - 为什么 (( s )) 进行变量查找,而不是 [[ s ]]?

c# - 解析 ANSI 转义码?

用于解析 Latex 或 MathML 字符串的 Java 或 scala 库

linux - 如何将/etc/passwd 存储在散列或数组中?

c++ -/usr/../libstdc++.so.6 : version `GLIBCXX_3.4.11' not found (required by . ..)

shell - 如何将Jenkins凭证传递给Dockerfile/Shell?

java - 以编程方式从给定化学式中分离原子