1

I am trying to parse the output below:

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

It is the list of flavours in open-stack. I am expecting output as below:

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

What I tried:

I came up with below command for parsing:

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

but the issue is that the command returns output as follows:

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

There is a problem with the space in new test flavor.

1
  • That looks like the output from some SQL flavour. You can usually make your life easier by producing correct output rather than parsing human-readable output. For example, in MySQL you could just SELECT Name and use --batch (and possibly --raw) to produce non-tabular output. With sqlite3, you could use -csv or -line. Commented Jul 1, 2016 at 18:06

3 Answers 3

2

Below command will give expected output

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

Above command will give output will white spaces i.e.

$ 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         ;

To get output without white spaces use below command

$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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.yes it gives output but think need to trim it . "m1.tiny ; m1.small ; MC ; m1.medium ; m1.large ; m1.xlarge ; slmc ; new test flavor ; slstore ;" there are specs before semicolon
0

Use the following command

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' ';'

Comments

0

Software tools (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

Pure bash (uses no utils at all):

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

Output (either one):

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.