b10n1k b10n1k - 3 months ago 8
Linux Question

get the values of a specific line from a specific column

what I want is to extract the list of a table with the following format

+---------+---------------+-------------+
| Region | Parent Region | Description |
+---------+---------------+-------------+
| region1 | None | |
| region2 | None | |
+---------+---------------+-------------+


From region I want to get region1, region2.

what I have tried so far is

$ cat file |cut -d'+' -f1|cut -d'|' -f2

Region

region1


Any idea?

sed command which returns

Answer

I am not sure if sed is the right tool for this instead a native DB tool, but here you go:

sed -nE '3,$ s/^\|[[:blank:]]*([^[:blank:]|]*).*/\1/p' file.txt
  • 3,$ does the operation only from line 3 till end

  • ^\|[[:blank:]]*([^[:blank:]|]*).* matches | at the start and then whitespaces (if any) and the captured group contains the desired portion

  • In the replacement we have used the first (only) captured group, \1

Example:

$ cat file.txt
+---------+---------------+-------------+
| Region  | Parent Region | Description |
+---------+---------------+-------------+
| region1 | None          |             |
| region2 | None          |             |
+---------+---------------+-------------+

$ sed -nE '3,$ s/^\|[[:blank:]]*([^[:blank:]|]*).*/\1/p' file.txt
region1
region2
Comments