通过从每行中删除相同的短语来解析文件,然后按字母顺序排序结果



这是我的输入文件ATM,它具有1000加行:

system="Tom Sawyer Rose"
system="A Far Coat"
system="Be Nice To Her"
system="Oh Yes"
system="Zebra Heaven"
system="O Boy"
system="Too Bad Sunny"
system="Felix Rocks"

我想删除单词系统,平等符号和报价:

Tom Sawyer Rose
A Far Coat
Be Nice To Her
Oh Yes
Zebra Heaven
O Boy
Too Bad Sunny
Felix Rocks

然后我想按字母顺序排序:

A Far Coat
Be Nice To Her
Felix Rocks
O Boy
Oh Yes
Tom Sawyer Rose
Too Bad Sunny
Zebra Heaven

对于该数据cutsort使用"作为字段定界符并提取第二个字段或列时,就足够了:

$ cut -d " -f 2 file | sort
A Far Coat
Be Nice To Her
Felix Rocks
...

使用sedsort

~ $ sed 's/^system="(.*)"$/1/' input.txt | sort
A Far Coat
Be Nice To Her
Felix Rocks
O Boy
Oh Yes
Tom Sawyer Rose
Too Bad Sunny
Zebra Heaven

sidenote:请考虑下次有类似问题时查找这些工具,并始终将失败的尝试包括在问题描述中。否则,您会赢得自己的低价,因为人们认为您懒得自己寻找解决方案。

$ awk -F"[=|"]" '{print $3}' input | sort

简短说明,

  • -F"[=|"]:将输入字段分隔符设置为=或"
  • 按您想要的$ 3打印
  • 管道awk结果 sort

,或者您也可以使用grep来做到这一点,

grep -oP '="K.*(?=")' input | sort

您可以尝试以下内容,让我知道是否有帮助。

解决方案1st:通过使用sub和gsub实用程序和sort。

awk '{sub(/[^"]*/,"");gsub(/"/,"");print | "sort"}'  Input_file

输出将如下。

A Far Coat
Be Nice To Her
Felix Rocks
O Boy
Oh Yes
Tom Sawyer Rose
Too Bad Sunny
Zebra Heaven

编辑:现在也添加解释和非一线衬里形式。

awk '{
sub(/[^"]*/,""); ##Using awk default utility named sub for substitution which works on sub(/regex/,new_text/variable,line/variable), so substituting everything till first occurence of " with NULL here in current line.
gsub(/"/,"");    ##Now using gsub(globally substitution) method to remove all remaining occurrences of " with NULL in current line.
print | "sort"   ##Now using print of current line, here point to be noted | "sort" will take all the lines of Input_file and sort them(like normal sort with 1st column alphabetic order).
}
' Input_file     ##Mentioning the Input_file name here.

解决方案2:将"(字符串)作为字段分隔器,然后对其进行排序。

awk -F'"' '{print $2 | "sort"}'  Input_file
A Far Coat
Be Nice To Her
Felix Rocks
O Boy
Oh Yes
Tom Sawyer Rose
Too Bad Sunny
Zebra Heaven

相关内容

最新更新