bash – 如何将不以特定模式开头的行连接到UNIX中的上一行?
发布时间:2020-12-16 01:53:04 所属栏目:安全 来源:网络整理
导读:请查看下面的示例文件和所需的输出,以了解我在寻找什么. 它可以用shell脚本中的循环完成,但我很难得到一个awk / sed一个衬垫. SampleFile.txt These are leaves.These are branches.These are greenery which givesoxygen,provides control over temperature
请查看下面的示例文件和所需的输出,以了解我在寻找什么.
它可以用shell脚本中的循环完成,但我很难得到一个awk / sed一个衬垫. SampleFile.txt These are leaves. These are branches. These are greenery which gives oxygen,provides control over temperature and maintains cleans the air. These are tigers These are bears and deer and squirrels and other animals. These are something you want to kill Which will see you killed in the end. These are things you must to think to save your tomorrow. 期望的输出 These are leaves. These are branches. These are greenery which gives oxygen,provides control over temperature and maintains cleans the air. These are tigers These are bears and deer and squirrels and other animals. These are something you want to kill Which will see you killed in the end. These are things you must to think to save your tomorrow. 解决方法
请尝试以下方法:
awk 'BEGIN {accum_line = "";} /^These/{if(length(accum_line)){print accum_line; accum_line = "";}} {accum_line = accum_line " " $0;} END {if(length(accum_line)){print accum_line; }}' < data.txt 代码由三部分组成: >标记为BEGIN的块先于其他任何操作执行.它对全局初始化很有用>标记为END的块在常规处理完成时执行.包装好东西很好.就像打印最后收集的数据一样,如果这行在开头没有这些(这种情况)>其余的是为每一行执行的代码.首先,搜索模式并完成相关的事情.其次,无论字符串内容如何,??都会进行数据收集. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |