我是unix的新手,需要一些帮助...
我有一个包含以下内容的文件:
119000 ABC/CSD/NEW/PB/PB1234_PB0001123.CSV
60000 ABC/CSD/NEW/PB/PB14567_PB0001123.CSV
25000 ABC/CSD/NEW/PB/VV/PB16734_PB0001123.CSV
80000 ABC/CSD/NEW/PB/VV/PB2314_PB09820123.CSV
33117 ABC/CSD/NEW/PB/VV/PB45634_PB0001123.CSV
我希望输出如下:
119000 PB1234 PB0001123
60000 PB14567 PB0001123
25000 PB16734 PB0001123
80000 PB2314 PB09820123
33117 PB45634 PB0001123
按第二个字段排序,然后按第三个字段排序......
答案 0 :(得分:3)
如果您的实际Input_file与显示的示例相同,那么关注awk
可能对您有所帮助。
awk -F'[ /_.]' '{print $1,$(NF-2),$(NF-1)}' Input_file
<强> 说明: 强>
-F'[ /_.]'
:-F
在awk
中用于设置行的分隔符。所以我们在这里设置(空格),
/
,_
和.
作为提及(传递)Input_file的任何行的字段分隔符。
print $1,$(NF-2),$(NF-1)
:使用名为awk
的{{1}}开箱即用命令打印,然后将print
提及为当前行的第一个字段值,$1
提到当前行的第3个最后一个字段值$(NF-2)
表示当前行的第二个字段值。
答案 1 :(得分:1)
你可以试试这个:
awk '{
split($2,a,"/") # Split the 2nd element into the array a
split(a[length(a)],b,"[_.]") # Split the last element of the array into the array b
print $1,b[1],b[2] # Print the wanted string
}' file