Text-Processing

使用 awk 或 sed 擷取和解析文本

  • April 27, 2022

我有一些文本輸出,如下所示:

+-------------------------+-----+--------------------------------+--------------------------------------------+
| Site                    | SSL | URL                            | Path                                       |
+-------------------------+-----+--------------------------------+--------------------------------------------+
| samplerds.test          |     | http://samplerds.test          | /Users/user01/Paths/samplexx1              |
| wpo.test                |     | http://wpo.test                | /Users/user01/Paths/wpo                    |
+-------------------------+-----+--------------------------------+--------------------------------------------+

有什麼方法可以使用awksed將它們轉換為以下文本來解析上面的文本:

samplerds
wpo

乾杯

使用awk

$ awk -F '[ .]' 'NR>3 && !/^\+/ {print $2}' file
samplerds
wpo

或將子域添加為欄位分隔符:

awk -F ' |\.test' 'NR>3 && !/^\+/ {print $2}' file

或者gsub當你有子域時讓它也能工作:

awk 'NR>3 && !/^\+/ {gsub("\.[^.]*$","",$2); print $2}' file

引用自:https://unix.stackexchange.com/questions/700568