簡體   English   中英

sed逆序標簽讀取

[英]sed reverse order label reading

我想使用sed解析文件,並僅在字段中打印最后的i標簽。 每個標簽用分隔.

如果我選擇i=3 ,並且文件包含以下幾行:

begin_text|label_n.label_n-1.other_labels.label3.label2.label1|end_text
BEGIN_TEXT|LABEL3.LABEL2.LABEL1|END_TEXT
Begin_Text|Label2.Label1|End_Text

我想,如果至少有3個標簽,則輸出行應為:

begin_text|label_n.label_n-1.other_labels.label3.label2.label1|end_text
BEGIN_TEXT|LABEL3.LABEL2.LABEL1|END_TEXT

目前 :

sed 's;\(^[^|]\+\)|.*\.\([^\.]\+\.[^\.]\+\.[^\.]\+\)|\([^|]\+$\);\1|\2|\3;' test.txt

生產:

begin_text|label3.label2.label1|end_text
BEGIN_TEXT|LABEL3.LABEL2.LABEL1|END_TEXT
Begin_Text|Label2.Label1|End_Text

並且我不知道為什么第3行會發生匹配。我還認為這是進行逆序標簽讀取的更好方法。

任何意見/建議表示贊賞。

使用awk將使工作更加輕松。

awk 'split($2,a,".")>=i' FS="|" i=3 file

begin_text|label_n.label_n-1.other_labels.label3.label2.label1|end_text
BEGIN_TEXT|LABEL3.LABEL2.LABEL1|END_TEXT

說明

split(string, array, fieldsep)
split returns the number of elements created. 

暫無
暫無

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

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