I have a text:
+--------------------------------------+
| ID |
+--------------------------------------+
| cb91a52f-f0dd-443a-adfe-84c5c685d9b3 |
| 184564aa-9a7d-48ef-b8f0-ff9d51987e71 |
| f01f9739-c7a7-404c-8789-4e3e2edf314e |
| 825925cc-a816-4434-8b4b-a75301ddaefd |
| 5b1b0ec1-9c7e-4852-bbb7-b1c883c2acce |
| fec4a447-9f51-4796-9c92-42268cd21ad1 |
| 77b37392-c5f8-4537-8e2b-971f6b161dec |
| e8524cf0-47d4-43f8-aee3-2af03f511ebe |
| 2a6640e2-bfdf-416c-8d86-e2127802779a |
| 0223ac3d-2f99-42ab-b6be-cc738e5e3e25 |
| d3577eb1-c8cb-4328-bf5f-48cb2ef43229 |
| f46ed4c1-9325-4d35-8dda-18d2af31a510 |
| 311bc474-5747-4408-9124-6fa3a24c08c1 |
| 81ebddc6-612f-4675-9004-80c35207e1e3 |
| 09518994-7b37-481c-b058-d74c16d4c198 |
| 1e318d02-afd4-4b97-85d8-8c8f8b007144 |
| c220fae1-5a56-485f-ad51-0e0316bc105c |
| afbef917-2ee8-4eb9-948d-48fcd5ffc6e4 |
| cc8a6692-2ba9-4203-bf50-b02a8e4a0f21 |
| f49089be-4b2a-4b10-bfbd-495001e64f54 |
| d89b0b19-0f26-4b9a-8a9a-25a41f59cee5 |
| 18f1717f-74ac-429a-ba2b-4466489a2558 |
| 7560d8b2-3e3d-474c-bcf8-ded378d50954 |
| 1b02c8f9-6077-4499-93f6-bc04d8b0ca3a |
| 9ca256b4-c9eb-4a60-8fb5-dfe4ea883e1f |
| 3c837469-aa82-41ac-b75e-9bb087501789 |
| e151d2c7-5360-466c-857f-7474eec4eaeb |
| c7ca1066-c077-4846-aae0-56d7fc63c32d |
| 034026df-9e45-49e1-ab41-0ad3f4e5e33b |
| 45bb533d-ff92-419e-8523-bb4c9889a04e |
+--------------------------------------+
**** Total: 30 ****
I want to catch lines like this:
| cb91a52f-f0dd-443a-adfe-84c5c685d9b3 |
which is "| UUID |"
So I use regular like this:
check_vm_connectivity()
{
res=`cat temp.txt` # this got text above
re='^\| [a-z0-9\-]+ \|'
for line in "$res"
do
if [[ "${line}" =~ $re ]] ; then
echo "${line}"
fi
done
}
^\| [a-z0-9\-]+ \| is the regular I use, but the result capture all lines, I don't know what's wrong? Is that because I should change ' into "?
Would the following command be what you are looking for:
grep -oP "^\|\s*\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\s*\|$" input.file | grep -v '_'
OUTPUT on your input file:
| cb91a52f-f0dd-443a-adfe-84c5c685d9b3 |
| 184564aa-9a7d-48ef-b8f0-ff9d51987e71 |
| f01f9739-c7a7-404c-8789-4e3e2edf314e |
| 825925cc-a816-4434-8b4b-a75301ddaefd |
| 5b1b0ec1-9c7e-4852-bbb7-b1c883c2acce |
| fec4a447-9f51-4796-9c92-42268cd21ad1 |
| 77b37392-c5f8-4537-8e2b-971f6b161dec |
| e8524cf0-47d4-43f8-aee3-2af03f511ebe |
| 2a6640e2-bfdf-416c-8d86-e2127802779a |
| 0223ac3d-2f99-42ab-b6be-cc738e5e3e25 |
| d3577eb1-c8cb-4328-bf5f-48cb2ef43229 |
| f46ed4c1-9325-4d35-8dda-18d2af31a510 |
| 311bc474-5747-4408-9124-6fa3a24c08c1 |
| 81ebddc6-612f-4675-9004-80c35207e1e3 |
| 09518994-7b37-481c-b058-d74c16d4c198 |
| 1e318d02-afd4-4b97-85d8-8c8f8b007144 |
| c220fae1-5a56-485f-ad51-0e0316bc105c |
| afbef917-2ee8-4eb9-948d-48fcd5ffc6e4 |
| cc8a6692-2ba9-4203-bf50-b02a8e4a0f21 |
| f49089be-4b2a-4b10-bfbd-495001e64f54 |
| d89b0b19-0f26-4b9a-8a9a-25a41f59cee5 |
| 18f1717f-74ac-429a-ba2b-4466489a2558 |
| 7560d8b2-3e3d-474c-bcf8-ded378d50954 |
| 1b02c8f9-6077-4499-93f6-bc04d8b0ca3a |
| 9ca256b4-c9eb-4a60-8fb5-dfe4ea883e1f |
| 3c837469-aa82-41ac-b75e-9bb087501789 |
| e151d2c7-5360-466c-857f-7474eec4eaeb |
| c7ca1066-c077-4846-aae0-56d7fc63c32d |
| 034026df-9e45-49e1-ab41-0ad3f4e5e33b |
| 45bb533d-ff92-419e-8523-bb4c9889a04e |
Explanations:
^ beginning of line\| match | literally \s* followed by 0 to N space char\w{8}-\w{4}-\w{4}-\w{4}-\w{12} where \w is a word char number, digit or underscore, \w{8} the 8 means appearing exactly 8 times, {12} means appearing exactly 12 times, ... \s* followed by 0 to N space char\| match | literally$ for the end of lineThen just in case I pipe to | grep -v '_' to remove lines with _ this might not be necessary if you are confident that the lines will not contain those one.
If you want to extract the UUID you can use the following grep command:
grep -oP "(?<=^\|\s)\w{8}-\w{4}-\w{4}-\w{4}-\w{12}(?=\s\|$)" input.file | grep -v '_'
cb91a52f-f0dd-443a-adfe-84c5c685d9b3
184564aa-9a7d-48ef-b8f0-ff9d51987e71
f01f9739-c7a7-404c-8789-4e3e2edf314e
825925cc-a816-4434-8b4b-a75301ddaefd
5b1b0ec1-9c7e-4852-bbb7-b1c883c2acce
fec4a447-9f51-4796-9c92-42268cd21ad1
77b37392-c5f8-4537-8e2b-971f6b161dec
e8524cf0-47d4-43f8-aee3-2af03f511ebe
2a6640e2-bfdf-416c-8d86-e2127802779a
0223ac3d-2f99-42ab-b6be-cc738e5e3e25
d3577eb1-c8cb-4328-bf5f-48cb2ef43229
f46ed4c1-9325-4d35-8dda-18d2af31a510
311bc474-5747-4408-9124-6fa3a24c08c1
81ebddc6-612f-4675-9004-80c35207e1e3
09518994-7b37-481c-b058-d74c16d4c198
1e318d02-afd4-4b97-85d8-8c8f8b007144
c220fae1-5a56-485f-ad51-0e0316bc105c
afbef917-2ee8-4eb9-948d-48fcd5ffc6e4
cc8a6692-2ba9-4203-bf50-b02a8e4a0f21
f49089be-4b2a-4b10-bfbd-495001e64f54
d89b0b19-0f26-4b9a-8a9a-25a41f59cee5
18f1717f-74ac-429a-ba2b-4466489a2558
7560d8b2-3e3d-474c-bcf8-ded378d50954
1b02c8f9-6077-4499-93f6-bc04d8b0ca3a
9ca256b4-c9eb-4a60-8fb5-dfe4ea883e1f
3c837469-aa82-41ac-b75e-9bb087501789
e151d2c7-5360-466c-857f-7474eec4eaeb
c7ca1066-c077-4846-aae0-56d7fc63c32d
034026df-9e45-49e1-ab41-0ad3f4e5e33b
45bb533d-ff92-419e-8523-bb4c9889a04e
Your regular expression re='^\| [a-z0-9\-]+ \|' is fine, the problem in your code is with the for loop going through it line by line.
for line in "$res"
That executes the loop only once, with the whole contents of $res inside $line.
This should fix it:
check_vm_connectivity() {
local re='^\| [a-z0-9\-]+ \|'
local line
while read line ; do
if [[ "${line}" =~ $re ]] ; then
echo "${line}"
fi
done <temp.txt
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With