为什么grep在使用正则表达式时会返回不同的结果?

时间:2016-01-27 18:22:15

标签: regex linux bash unix grep

> ps -ef | grep cron

root      1052     1  0 04:35 ?        00:00:00 cron
test   4071  4053  0 10:15 pts/0    00:00:00 grep --color=auto cron

> ps -ef | grep cr[o]n

root      1052     1  0 04:35 ?        00:00:00 cron

为什么当搜索词等效时,这两个命令会返回不同的结果?

3 个答案:

答案 0 :(得分:3)

因为在第二种情况下实际运行的进程是(在应用grep过滤器后你无法看到的第二种情况):

root      1052     1  0 04:35 ?        00:00:00 cron
test   4071  4053  0 10:15 pts/0    00:00:00 grep --color=auto cr[o]n

cr[o]ncr[o]n不匹配,因为[o]指定了字符列表o,而真正的字符串也包含[和{{1}围绕]

答案 1 :(得分:1)

命令

> ps -ef | grep cron

匹配字符串" cron"在命令中,给你

test   4071  4053  0 10:15 pts/0    00:00:00 grep --color=auto cron

然而,命令

> ps -ef | grep cr[o]n

与字符串" cr [o] n"不匹配在命令本身中,从结果中省略它。

答案 2 :(得分:-1)

不是全部,如果多次重复该命令,两个命令类的输出将不同。原因是输出取决于ps获取信息时正在执行的grep命令。 Bash shell只是按快速顺序启动这两个命令,但有时private ImageView partyIcon; private ImageView dealIcon; private ImageView deliveryIcon; private TextView txtParty; private TextView txtDeals; private TextView txtDelivery; boolean doubleBackToExitPressedOnce = false; int backButtonCount = 0; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_after_login); Utility.setStatusBarColor(this, R.color.splash_status_color); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); partyIcon = (ImageView)findViewById(R.id.party_Icon); dealIcon = (ImageView)findViewById(R.id.deals_Icon); deliveryIcon = (ImageView)findViewById(R.id.delivery_Icon); partyIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplication(), BookPartyActivity.class); startActivity(intent); } }); dealIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplication(), DealsActivity.class); startActivity(intent); } }); deliveryIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LoginBean loginBean = new LoginBean(); Toast.makeText(getBaseContext(),"Auth"+loginBean.getUser_id(),Toast.LENGTH_LONG).show(); Intent intent = new Intent(getApplicationContext(),MyAuction.class); startActivity(intent); } }); } /* public void onBackPressed() { if (doubleBackToExitPressedOnce) { Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); } doubleBackToExitPressedOnce = true; Toast.makeText(this, "you have logged in ,plz enjoy the party", Toast.LENGTH_LONG).show(); new Handler().postDelayed(new Runnable() { public void run() { doubleBackToExitPressedOnce = false; } } , 2000L); }*/ @Override public void onBackPressed() { if(backButtonCount >= 1) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } else { Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show(); backButtonCount++; } } 命令会在public void setUser_id(String user_id) { this.user_id = user_id; } public String getUser_id() { return user_id; } public void setFull_name(String full_name) { this.full_name = full_name; } public String getFull_name() { return full_name; } public void setDisplay_name(String display_name) { this.display_name = display_name; } public String getDisplay_name() { return display_name; } public void setUser_image(String user_image) { this.user_image = user_image; } public String getUser_image() { return user_image; } public void setGender(String gender) { this.gender = gender; } public String getGender() { return gender; } public void setAuthorization_key(String authorization_key) { this.authorization_key = authorization_key; } public String getAuthorization_key() { return authorization_key; } 没有时间启动ps(1)时获取信息。

两个正则表达式完全相同,因为bash(1)grep(1)完全相同。

多次尝试启动cr[o]n并查看有时会出现grep命令,有时不会出现。

相关问题