将句子分成单独的行
我正在尝试使用 shell 脚本将文件中的句子拆分为单独的行。
现在我想用 !, 分割字符串。或者 。. 输出应该是这样的:
我想从my_text.txt读取并包含的文件
you want to learn shell script? First, you want to learn Linux command! then. you can learn shell script.
现在我想用“!”或“?”或“。”分割字符串。输出应该是这样的:
you want to learn shell script First, you want to learn Linux command then you can learn shell script
我使用了这个脚本:
while read p
do
echo $p | tr "? ! ." "n "
done < my_text.txt
但输出是:
你想学习shell脚本
首先,你想学习Linux命令然后你可以学习shell脚本
有人可以帮忙吗?
回答
这可以awk使用其全局替换选项单独完成,如下所示,仅在 GNU 中使用显示的示例编写和测试awk。简单地全局代?,!,.用新的行(默认ORS(输出记录分隔符)的值作为新的线)。
awk '{gsub(/?|!|./,ORS)} 1' Input_file