如何提取字符串组并将其附加到文件的底部

时间:2017-06-13 07:44:54

标签: unix awk sed paste cut

美好的一天,

我有一个显示如下的文件:

Bin   1 = 5778    669  753  731  743  752  727  736  667    
Bin   5 =    2      2    0    0    0    0    0    0    0     Bin   7 =   63     36    5    1    1    5    1    5    9    
Bin   9 =    0      0    0    0    0    0    0    0    0     Bin  24 =    2      0    0    0    0    0    0    0    2    
Bin  31 =    0      0    0    0    0    0    0    0    0     Bin  47 =   28      2    4    6    1    2    5    3    5    
Bin  70 =    1      0    0    0    0    0    1    0    0     Bin  95 =    1      0    1    0    0    0    0    0    0    
Bin 120 =    1      0    1    0    0    0    0    0    0     Bin 121 =  162     49   13   11   10    3   10   12   54    
Bin 131 =    7      1    1    2    0    0    1    0    2     Bin 133 =    3      0    0    0    0    0    0    0    3    
Bin 141 =    6      0    0    0    1    0    1    2    2     Bin 560 =    1      0    0    0    1    0    0    0    0    
Bin 620 =    1      0    0    0    1    0    0    0    0     Bin 630 =    2      0    0    1    0    0    0    0    1    
Bin 1250 =    1     0    0    0    0    0    1    0    0     Bin 1410 =    1     0    1    0    0    0    0    0    0    
Bin 2004 =    1     0    0    0    0    0    0    1    0     Bin 2240 =    1     0    0    0    0    1    0    0    0    
Bin 2247 =    1     0    0    1    0    0    0    0    0     Bin 2300 =    1     0    0    0    0    1    0    0    0    
Bin 2309 =    1     0    0    0    1    0    0    0    0     Bin 2310 =    3     1    1    0    0    0    0    0    1    
Bin 2602 =    1     0    0    0    1    0    0    0    0    

我想在右侧剪切第二个“Bin”模式及其值,然后将其粘贴到文件的底部,然后再对其进行排序。感谢你对我这个问题的帮助。我是unix命令的新手,还在学习它。提前谢谢。

我想有这样的输出。

Bin   1 = 5778    669  753  731  743  752  727  736  667    
Bin   5 =    2      2    0    0    0    0    0    0    0     
Bin   7 =   63     36    5    1    1    5    1    5    9    
Bin   9 =    0      0    0    0    0    0    0    0    0     
Bin  24 =    2      0    0    0    0    0    0    0    2    
Bin  31 =    0      0    0    0    0    0    0    0    0     
Bin  47 =   28      2    4    6    1    2    5    3    5    
Bin  70 =    1      0    0    0    0    0    1    0    0     
Bin  95 =    1      0    1    0    0    0    0    0    0    
Bin 120 =    1      0    1    0    0    0    0    0    0     
Bin 121 =  162     49   13   11   10    3   10   12   54    
Bin 131 =    7      1    1    2    0    0    1    0    2     
Bin 133 =    3      0    0    0    0    0    0    0    3    
Bin 141 =    6      0    0    0    1    0    1    2    2     
Bin 560 =    1      0    0    0    1    0    0    0    0    
Bin 620 =    1      0    0    0    1    0    0    0    0     
Bin 630 =    2      0    0    1    0    0    0    0    1    
Bin 1250 =    1     0    0    0    0    0    1    0    0     
Bin 1410 =    1     0    1    0    0    0    0    0    0    
Bin 2004 =    1     0    0    0    0    0    0    1    0     
Bin 2240 =    1     0    0    0    0    1    0    0    0    
Bin 2247 =    1     0    0    1    0    0    0    0    0     
Bin 2300 =    1     0    0    0    0    1    0    0    0    
Bin 2309 =    1     0    0    0    1    0    0    0    0     
Bin 2310 =    3     1    1    0    0    0    0    0    1    
Bin 2602 =    1     0    0    0    1    0    0    0    0

方面,麦克

3 个答案:

答案 0 :(得分:2)

awk 方法:

awk 'NF>12{ $13= ORS $13 }1' file | column -t
  • NF - 字段总数

  • NF>12 - 根据您的预期结果,最大字段数为12. 非空白序列被视为字段

  • $13= ORS $13 - 使用记录分隔符ORS(换行符)分隔第13个字段的记录

输出:

Bin  1     =  5778  669  753  731  743  752  727  736  667
Bin  5     =  2     2    0    0    0    0    0    0    0
Bin  7     =  63    36   5    1    1    5    1    5    9
Bin  9     =  0     0    0    0    0    0    0    0    0
Bin  24    =  2     0    0    0    0    0    0    0    2
Bin  31    =  0     0    0    0    0    0    0    0    0
Bin  47    =  28    2    4    6    1    2    5    3    5
Bin  70    =  1     0    0    0    0    0    1    0    0
Bin  95    =  1     0    1    0    0    0    0    0    0
Bin  120   =  1     0    1    0    0    0    0    0    0
Bin  121   =  162   49   13   11   10   3    10   12   54
Bin  131   =  7     1    1    2    0    0    1    0    2
Bin  133   =  3     0    0    0    0    0    0    0    3
Bin  141   =  6     0    0    0    1    0    1    2    2
Bin  560   =  1     0    0    0    1    0    0    0    0
Bin  620   =  1     0    0    0    1    0    0    0    0
Bin  630   =  2     0    0    1    0    0    0    0    1
Bin  1250  =  1     0    0    0    0    0    1    0    0
Bin  1410  =  1     0    1    0    0    0    0    0    0
Bin  2004  =  1     0    0    0    0    0    0    1    0
Bin  2240  =  1     0    0    0    0    1    0    0    0
Bin  2247  =  1     0    0    1    0    0    0    0    0
Bin  2300  =  1     0    0    0    0    1    0    0    0
Bin  2309  =  1     0    0    0    1    0    0    0    0
Bin  2310  =  3     1    1    0    0    0    0    0    1
Bin  2602  =  1     0    0    0    1    0    0    0    0

答案 1 :(得分:1)

sed 's/ \(Bin.*\)$/\n\1/g' file | sort -k 2 -n > outputfile

这将搜索以空格开头的字符串,后跟" Bin"以及直到剩余线路的任何角色 可以使用\1在命令的替换部分中引用放入转义的parantheses的部分。在那里你插入一个换行符。

排序部分采用参数

  • -k 2(在第二列上排序)
  • -n(数字排序)

输出:

# sed 's/ \(Bin.*\)$/\n\1/g' your_file | sort -k 2 -n
Bin   1 = 5778    669  753  731  743  752  727  736  667
Bin   5 =    2      2    0    0    0    0    0    0    0
Bin   7 =   63     36    5    1    1    5    1    5    9
Bin   9 =    0      0    0    0    0    0    0    0    0
Bin  24 =    2      0    0    0    0    0    0    0    2
Bin  31 =    0      0    0    0    0    0    0    0    0
Bin  47 =   28      2    4    6    1    2    5    3    5
Bin  70 =    1      0    0    0    0    0    1    0    0
Bin  95 =    1      0    1    0    0    0    0    0    0
Bin 120 =    1      0    1    0    0    0    0    0    0
Bin 121 =  162     49   13   11   10    3   10   12   54
Bin 131 =    7      1    1    2    0    0    1    0    2
Bin 133 =    3      0    0    0    0    0    0    0    3
Bin 141 =    6      0    0    0    1    0    1    2    2
Bin 560 =    1      0    0    0    1    0    0    0    0
Bin 620 =    1      0    0    0    1    0    0    0    0
Bin 630 =    2      0    0    1    0    0    0    0    1
Bin 1250 =    1     0    0    0    0    0    1    0    0
Bin 1410 =    1     0    1    0    0    0    0    0    0
Bin 2004 =    1     0    0    0    0    0    0    1    0
Bin 2240 =    1     0    0    0    0    1    0    0    0
Bin 2247 =    1     0    0    1    0    0    0    0    0
Bin 2300 =    1     0    0    0    0    1    0    0    0
Bin 2309 =    1     0    0    0    1    0    0    0    0
Bin 2310 =    3     1    1    0    0    0    0    0    1
Bin 2602 =    1     0    0    0    1    0    0    0    0

答案 2 :(得分:0)

如果文本是结构化的,那么cut就足够了,例如:

(cut -c1-56 infile; cut -c62- infile) | sort -k2n

输出:

Bin   1 = 5778    669  753  731  743  752  727  736  667
Bin   5 =    2      2    0    0    0    0    0    0    0
Bin   7 =   63     36    5    1    1    5    1    5    9    
Bin   9 =    0      0    0    0    0    0    0    0    0
Bin  24 =    2      0    0    0    0    0    0    0    2    
Bin  31 =    0      0    0    0    0    0    0    0    0
Bin  47 =   28      2    4    6    1    2    5    3    5    
Bin  70 =    1      0    0    0    0    0    1    0    0
Bin  95 =    1      0    1    0    0    0    0    0    0    
Bin 120 =    1      0    1    0    0    0    0    0    0
Bin 121 =  162     49   13   11   10    3   10   12   54    
Bin 131 =    7      1    1    2    0    0    1    0    2
Bin 133 =    3      0    0    0    0    0    0    0    3    
Bin 141 =    6      0    0    0    1    0    1    2    2
Bin 560 =    1      0    0    0    1    0    0    0    0    
Bin 620 =    1      0    0    0    1    0    0    0    0
Bin 630 =    2      0    0    1    0    0    0    0    1    
Bin 1250 =    1     0    0    0    0    0    1    0    0
Bin 1410 =    1     0    1    0    0    0    0    0    0    
Bin 2004 =    1     0    0    0    0    0    0    1    0
Bin 2240 =    1     0    0    0    0    1    0    0    0    
Bin 2247 =    1     0    0    1    0    0    0    0    0
Bin 2300 =    1     0    0    0    0    1    0    0    0    
Bin 2309 =    1     0    0    0    1    0    0    0    0
Bin 2310 =    3     1    1    0    0    0    0    0    1    
Bin 2602 =    1     0    0    0    1    0    0    0    0