如何在 unix 中打印仅包含整数和逗号的字符串?

how can i print a string that contains integers and commas only in unix?

我有一个包含以下内容的文件:

echo apple25,orange23,banana22 > file1

我正在尝试仅打印整数和逗号。

sed 's/[^0-9]*//g' file

想要的结果

25,23,22

你很接近;你只需要将逗号添加到你的字符 class.

sed 's/[^0-9,]*//g' file

正则表达式现在显示 "replace any characters that aren't 0-9 or a comma with nothing"。

使用 tr(假设您也需要换行符)。

$ tr -dc '[0-9],\n' <file