使用bash进行文本处理

时间:2011-11-12 18:14:03

标签: bash shell text-processing

我有一个vmstat转储文件,其中包含此格式的标头和值

procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------  
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st  
12  0 5924396 20810624 548548 935160    0    0     0     5    0    0 60  5 34  0  0  
12  0 5924396 20768588 548548 935160    0    0     0     0 1045 1296 99  0  0  0  0  
12  0 5924396 20768968 548548 935452    0    0     0    32 1025 1288 100  0  0  0  0  
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------  
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st  
 4  0 5924396 20768724 548552 935408    0    0     0    92 1093 1377 33  0 67  0  0  

我正在用bash编写一个脚本,它只提取包含数字行的行,即值,并删除包含字母表的所有行。我该怎么做

2 个答案:

答案 0 :(得分:1)

如果您只需要包含数字和制表符\空格的行,grep -P "^[0-9\ \t]*$"应该可以为您提供帮助。

$> cat ./text | grep -P "^[0-9\ \t]*$"
12  0 5924396 20810624 548548 935160    0    0     0     5    0    0 60  5 34  0  0  
12  0 5924396 20768588 548548 935160    0    0     0     0 1045 1296 99  0  0  0  0  
12  0 5924396 20768968 548548 935452    0    0     0    32 1025 1288 100  0  0  0  0  
 4  0 5924396 20768724 548552 935408    0    0     0    92 1093 1377 33  0 67  0  0  

答案 1 :(得分:0)

cat filename | grep "[0-9]"