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/ksh
INPUT=${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.
本文介绍了一个使用awk脚本来合并带有续行标记(-)的连续文本行的方法。通过具体的例子展示了如何处理文本文件中带有续行标识的情况,并提供了一个完整的awk脚本实现。

被折叠的 条评论
为什么被折叠?



