awk merge lines

来源:互联网 发布:java 统一异常处理 编辑:程序博客网 时间:2024/06/03 17:11

A sample to merge continuous text line using awk utility.


i.e, when a text line is ended with a dash ('-'), it should continue with next line.


For example:

 1:  11 2:  22 - 3:     33 4:  44


should be merged to:

 1:  11 2:  22 33 3:  44


The awk script:

#!/bin/kshINPUT=${1}awk '    function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }    function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }    function trim(s)  { return rtrim(ltrim(s)); }    BEGIN {}    {      # If the last char is a dash, continue      if (substr($0, length($0), 1) == "-") {        printf "%s ", rtrim(substr($0, 0, length($0) - 1))      }      else {        printf "%s\n", ltrim($0)      }    }    END {}' $INPUT


The End.

0 0
原创粉丝点击