繁体   English   中英

在Linux Shell脚本中解析

[英]Parsing in Linux shell script

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

+--------------------------------------+-----------------+-----------+------+-----------+------+-------+-------------+-----------+
| 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      |
+--------------------------------------+-----------------+-----------+------+-----------+------+-------+-------------+-----------+

它是开放式堆栈中的口味列表。 我期望输出如下:

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;

使用以下命令

sed -e '1,3d' < flavor-list | sed -e 's/-//g' | sed -e 's/+//g' | cut -f 3 -d "|" | sed -e 's/^ //g' | sed -e 's/\s\+$//g' | tr '\n' ';'

软件工具(no sed ,no awk ):

tail -n +4 flavor-list | grep -v '\-\-' | cut -d'|' -f3 | cut -d' ' -f2- | \
    tr -s ' ' | rev | cut -d' ' -f2- | rev | tr '\n' ';' ; echo

bash (完全不使用utils):

while read a b c d ; do \
    d="${d/ [ |]*/}" ; \
    [ -z "$d" -o "$d" = Name ] && continue ; \
    echo -n "$d;" ; \ 
done < flavor-list ; echo

输出(任一):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM