I want to find the column number of the string "net5-dummy1"
openstack server list | grep testserver01
Output :
| b2bcd3bd-a787-1c22-c0da-de98a7bek7788 | work-scc3-n3kdi955-scc01ltnmmk | ACTIVE | - | Running | net1-gw=10.70.1.11; net2-calico=10.71.22.12; net3-traffic=10.72.23.13; net4-trunk=171.16.8.12; net5-dummy1=171.19.8.12; net6-dummy2=10.88.8.25
Above output, need to find the column number of net5-dummy1. Kindly help me to identify the column number.
Thanks
CodePudding user response:
Assuming your columns are speratated by |
, you could do:
#!/bin/bash
input="| b2bcd3bd-a787-1c22-c0da-de98a7bek7788 | work-scc3-n3kdi955-scc01ltnmmk | ACTIVE | - | Running | net1-gw=10.70.1.11; net2-calico=10.71.22.12; net3-traffic=10.72.23.13; net4-trunk=171.16.8.12; net5-dummy1=171.19.8.12; net6-dummy2=10.88.8.25"
colnum=$(echo "$input" | tr '|' '\n' | grep -n "net5-dummy1" | cut -d':' -f1)
(( colnum -= 1 ))
echo "$colnum"
- Here I substract 1 to the the
grep -n
value since the first column is not the empty column before the first|
, I think.
Or if you also want to split columns on |
and ;
, you can do:
#!/bin/bash
input="| b2bcd3bd-a787-1c22-c0da-de98a7bek7788 | work-scc3-n3kdi955-scc01ltnmmk | ACTIVE | - | Running | net1-gw=10.70.1.11; net2-calico=10.71.22.12; net3-traffic=10.72.23.13; net4-trunk=171.16.8.12; net5-dummy1=171.19.8.12; net6-dummy2=10.88.8.25"
colnum=$(echo "$input" | tr '|' '\n' | tr ';' '\n' | grep -n "net5-dummy1" | cut -d':' -f1)
(( colnum -= 1 ))
echo "$colnum"