如何在linux中过滤搜索

时间:2016-04-14 02:18:17

标签: linux search filter

我有一个如下所示的数据文件:有时它们中的一些以“key M ###”开头(###代表任何数字),如何过滤我的搜索以便只包含那些包含“ M“会出现??

> dip binary_data_file.df | tail
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key M701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key M701, datafile binary_data_file.df, prt 0000000

2 个答案:

答案 0 :(得分:0)

<强> file.txt的:

key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key M701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key K701, datafile binary_data_file.df, prt 0000000
key M701, datafile binary_data_file.df, prt 0000000

使用grep

$ grep -E 'M[0-9]{3}' file.txt

或使用egrep

$ egrep 'M[0-9]{3}' file.txt

<强>输出:

key M701, datafile binary_data_file.df, prt 0000000
key M701, datafile binary_data_file.df, prt 0000000

答案 1 :(得分:0)

您可以将数据传递到while (game_over!=1): first_pick = input("Player1: pick any sticks up to 3 ") if first_pick > 3 or first_pick < 0: # <--- print('Please pick between 1-3 sticks') else: player1+=first_pick # <--- second_pick = input("Player2: pick any sticks up to 3") if second_pick > 3 or second_pick < 0: # <--- print('Please pick between 1-3 sticks') else: player2+=second_pick # <-- stick_left -= first_pick + second_pick for _ in range(5): print('| '*stick_left) 进行过滤。像下面的东西 -

grep