簡體   English   中英

使用AWK或SED等將文本文件分解為有組織的列表

[英]break down text file into an organized list using AWK or SED etc

將文本文件拆分為有組織的可讀文件的最佳方法是什么?

刪除所有不包含字符串JUNIOR或SENIOR的行后,我正在使用的文本文件采用以下格式:

<tr><td><a href="campers_SENIOR/head_unit">head_unit_1</a></td></tr>
<tr><td><a href="campers_JUNIOR/head_unit">head_unit_2</a></td></tr>
<tr><td><a href="campers_SENIOR/secondary_unit">secondary_unit_1</a></td></tr>
<tr><td><a href="campers_JUNIOR/secondary_unit">secondary_unit_2</a></td></tr>

我希望輸出為:

Unit Type: SENIOR
Unit Tier: head_unit
File Name: head_unit_1

Unit Type: SENIOR
Unit Tier: secondary_unit
File Name: secondary_unit_1

Unit Type: JUNIOR
Unit Tier: head_unit
File Name: head_unit_2

Unit Type: JUNIOR
Unit Tier: secondary_unit
File Name: secondary_unit_2

我一直在嘗試使用SED和AWK的混合物來實現這一目標。 我的問題是我不確定如何將其分為JUNIOR和SENIOR部分,以便更好地了解文件名和單位層。 請嘗試堅持使用SED和AWK解決方案,因為這些解決方案將是最有意義的,而且不會涉及太多。

如果您的輸入格式相對正確*,則將字段分隔符設置為[/"<> ]+將會提取您需要的信息:

$ awk -F'[/"<> ]+' '{sub("campers_", "", $6); print $6, $7, $8}' file
SENIOR head_unit head_unit_1
JUNIOR head_unit head_unit_2
SENIOR secondary_unit secondary_unit_1
JUNIOR secondary_unit secondary_unit_2

從那里開始,根據需要形成每個記錄很簡單。


*如果實際輸入的格式不如摘錄中的格式,則需要使用適當的HTML解析器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM