我正在尝试编写一个带有文件名的bash脚本,并返回包含一个单词的行.这是示例文本:
This has more than one word
There
is exactly one word in above line.
White-space
in the start of the above line doesn't matter.
Need-some-help.
Run Code Online (Sandbox Code Playgroud)
输出:
There
White-space
Need-some-help.
Run Code Online (Sandbox Code Playgroud)
我正在研究使用SED和Regex的组合.
注意:我不能使用任何其他东西(它必须是一个bash脚本,没有自定义模块),所以建议这样做无济于事.
如果单词可以包含任何非空白字符,则:
grep -E '^\s*\S+\s*$'
Run Code Online (Sandbox Code Playgroud)
要么
sed -E '/^\s*\S+\s*$/!d'
Run Code Online (Sandbox Code Playgroud)
要么
sed -n -E '/^\s*\S+\s*$/p'
Run Code Online (Sandbox Code Playgroud)