如何仅获取以“1”开头的文件中的第一行?后跟除数字以外的任何字符?

How can I get only the first line in a file that starts with "1." followed by any character except a number?

我有这个文件

1.1some text
2.some text
1.line I need

如何只打印文件中以“1”开头的第一行。后跟除数字以外的任何字符? 我希望这样:

1.line I need

我的代码是这样的

q=$(grep "^[0-9].[a-z]"  "file")
echo $q

谢谢

使用您显示的示例,请尝试以下 grep 代码。简单的解释是:使用 grepm1 选项仅打印第一个匹配项并退出程序,然后在主程序中提及正则表达式以匹配从 1. 开始后跟非 - 的行数字字符,如果找到匹配则打印该行。

grep -m1 '^1\.[^0-9]'  Input_file

How can I print only the first line in a file that start with 1.followed by any character except a number?

使用sed,您可以使用:

sed '/^1\.[^0-9]/!d;q' file

1.line I need

详情:

  • -n: 抑制常规输出
  • /^1\.[^0-9]/:搜索以 1 开头后跟一个点和一个非数字
  • 的行
  • !d:删除所有不匹配的行
  • q: 退出进一步处理

awk 中的类似解决方案是:

awk '/^1\.[^0-9]/{print; exit}' file