我有一个文本文件,我想删除其中包含的所有数字。但是,有两个关键字符串" 9/11"和#9; 9月11日",我想保留数字。除了它们是这些关键字符串的一部分外,如何删除所有数字?
我使用sed final Context context = this;
Button test_button = (Button) findViewById(R.id.button_test);
test_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Quit");
alertDialogBuilder
.setMessage("Are you sure want to Quit?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
menu.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
来摆脱数字。所以现在,处理之前的示例文本将是这样的:
's/[0-9]*//g'
我希望处理后的文件看起来像这样:
12 Aug. 2002, News Section. 9/11 was a terrible tragedy for the nation, in which 2,500 ...
我试着寻找答案,但无济于事。提前感谢任何建议。
答案 0 :(得分:1)
这将完成这项工作。这就像捕捉我们想要留下的部分并匹配您想要移除的部分。因此,通过将所有匹配的字符替换为组索引1中存在的字符,将使捕获的字符保持不变,其他匹配的字符将离开。
sed 's~\(\b9/11\b\|\bSeptember 11\b\)\|[[:digit:]]~\1~g' file