我有 FILE 的示例:
Rec 1 20014567
Rec 1 20014567
Rec 1 20014567
Rec 1 20014577
Rec 1 20014577
Rec 1 20014577
Rec 1 20014577
Rec 1 20014587
.
.
.
我需要根据这些要求拆分此文件:
输出 File1 :
Rec 1 20014567
Rec 1 20014567
Rec 1 20014567
输出 File2 :
Rec 1 20014577
Rec 1 20014577
Rec 1 20014577
Rec 1 20014577
输出 File3 :
Rec 1 20014587
我发现了这个:
sort -k 11.STARTPOS,14.ENDPOS
...可以按位置排序,但不能按文件过滤。
需要你帮助解决这个问题。
答案 0 :(得分:1)
You can use awk
for this:
awk '{print $0 >>substr($0,11,4)".txt"}' infile.txt
That will print the full record ($0) to a file that is named according to the substring of the record from position 11 for 4 characters.
Using the >>
to Append the data to the files, this way there is no need for sort. If you want the data sorted in each file, then just run the sort command first and pipe that to this awk script. Super simple.