需要在段落中为linux文本文件添加段落标题

时间:2015-10-10 20:02:08

标签: bash command-line data-manipulation

(使用Linux)我有许多文本文件,其中包含标题,后跟几行信息。我需要将标题移动到"段落#34;。

的每一行
Paragraph1Header
P1L1Col1  P1L1Col2
P1L2Col1  P1L2Col2

Paragraph2Header
P2L1Col1  P2L1Col2
P2L2Col1  P2L2Col2

P1L1Col1  Paragraph1Header P1L1Col2
P1L2Col1  Paragraph1Header P1L2Col2
P2L1Col1  Paragraph2Header P2L1Col2
P2L2Col1  Paragraph2Header P2L2Col2

1 个答案:

答案 0 :(得分:1)

将程序放入文件 pr.awk 并运行awk -f pr.awk input.txt

NF == 1 {
    h = $1
    next
}

NF > 1 {
    match($0, "[[:space:]]+")
    i = RSTART + RLENGTH - 1
    fst = substr($0,   1, i)
    rst = substr($0, i+1)
    print fst h " " rst
}