Removing trailing / starting newlines with sed, awk, tr, and friends

后端 未结 16 684
一个人的身影
一个人的身影 2021-01-30 21:04

I would like to remove all of the empty lines from a file, but only when they are at the end/start of a file (that is, if there are no non-empty lines before them, at the start;

16条回答
  •  爱一瞬间的悲伤
    2021-01-30 21:21

    using awk:

    awk '{a[NR]=$0;if($0 && !s)s=NR;}
        END{e=NR;
            for(i=NR;i>1;i--) 
                if(a[i]){ e=i; break; } 
            for(i=s;i<=e;i++)
                print a[i];}' yourFile
    

提交回复
热议问题