如果下一行不是以数字开头,则删除新行 [英] remove new line if next line does not begin with a number

查看:335
本文介绍了如果下一行不是以数字开头,则删除新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,就像

 
1 test
test

如何从最终输出中删除新行:


1测试测试



我已经尝试了一些sed,但是我无法正常工作。

解决方案

如果以数字开头,您可以更智能一些,并在行前打印新行line);

  awk'BEGIN {ORS =;} NR == 1 {print;下一个; } / ^ [[:digit:]] / {print\\\
;打印;下一个; } {print; }'

awk脚本:

  BEGIN {ORS =; }#默认:输出记录之间不换行
NR == 1 {print;下一个; }#第一行:print
/ ^ [[:digit:]] / {print\\\
;打印;下一个; }#如果以数字开始:
{print;}#其他行(next;还没有被调用)之前的print newline


I have a file that is like

    1 test
    test

How can I remove the new line from the so the final output becomes:

1 test test

I already tried some sed but I could not het it to work.

解决方案

You can be a bit smarter and print a new line before the line if it starts with a digit (except for the first line);

awk 'BEGIN{ORS="";} NR==1 { print; next; } /^[[:digit:]]/ { print "\n"; print; next; } { print; }'

The awk script:

BEGIN { ORS=""; }                            # default: no newline between output records
NR==1 { print; next; }                       # first line: print
/^[[:digit:]]/ { print "\n"; print; next; }  # if starts with a digit: print newline before
{print;}                                     # other lines (next; has not been called yet)

这篇关于如果下一行不是以数字开头,则删除新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆