在文本文件中添加标题

时间:2018-05-16 20:34:03

标签: bash awk sed

下面的目的是在一个大文件中添加标题,并生成按标题分割的页面。

标题需要每7行添加一次,并且可以选择增加页面中的数字。表示每次添加一个标题时,页面中的数字都会增加。

标题

----------------------------------------------------
 Code       10       20       30       40     Page 1
----------------------------------------------------

输入文件

 2917        2        0        0        0  =      2
 2916        0        0        0        5  =      5
 2915        2        0        3        0  =      5
 2914        2        2        1        0  =      5
 2912        0        2        0        0  =      2
 2911        0        2        0        0  =      2
 2910        3        0        0        0  =      3
 2917        2        0        0        0  =      2
 2916        0        0        0        5  =      5
 2915        2        0        3        0  =      5
 2914        2        2        1        0  =      5
 2912        0        2        0        0  =      2
 2911        0        2        0        0  =      2
 2910        3        0        0        0  =      3
 2917        2        0        0        0  =      2
 2916        0        0        0        5  =      5
 2915        2        0        3        0  =      5
 2914        2        2        1        0  =      5
 2912        0        2        0        0  =      2
 2911        0        2        0        0  =      2
 2910        3        0        0        0  =      3
 2917        2        0        0        0  =      2
 2916        0        0        0        5  =      5
 2915        2        0        3        0  =      5
 2914        2        2        1        0  =      5
 2912        0        2        0        0  =      2
 2911        0        2        0        0  =      2
 2910        3        0        0        0  =      3
 2917        2        0        0        0  =      2
 2916        0        0        0        5  =      5
 2915        2        0        3        0  =      5
 2914        2        2        1        0  =      5
 2912        0        2        0        0  =      2
 2911        0        2        0        0  =      2
 2910        3        0        0        0  =      3

输出文件

 Code       10       20       30       40     Page 1
----------------------------------------------------
 2917        2        0        0        0  =      2
 2916        0        0        0        5  =      5
 2915        2        0        3        0  =      5
 2914        2        2        1        0  =      5
 2912        0        2        0        0  =      2
 2911        0        2        0        0  =      2
 2910        3        0        0        0  =      3
----------------------------------------------------
Code       10       20       30       40     Page 2
----------------------------------------------------
 2917        2        0        0        0  =      2
 2916        0        0        0        5  =      5
 2915        2        0        3        0  =      5
 2914        2        2        1        0  =      5
 2912        0        2        0        0  =      2
 2911        0        2        0        0  =      2
 2910        3        0        0        0  =      3
----------------------------------------------------
Code       10       20       30       40     Page 3
----------------------------------------------------
 2917        2        0        0        0  =      2
 2916        0        0        0        5  =      5
 2915        2        0        3        0  =      5
 2914        2        2        1        0  =      5
 2912        0        2        0        0  =      2
 2911        0        2        0        0  =      2
 2910        3        0        0        0  =      3
----------------------------------------------------
Code       10       20       30       40     Page 4
----------------------------------------------------
 2917        2        0        0        0  =      2
 2916        0        0        0        5  =      5
 2915        2        0        3        0  =      5
 2914        2        2        1        0  =      5
 2912        0        2        0        0  =      2
 2911        0        2        0        0  =      2
 2910        3        0        0        0  =      3
----------------------------------------------------
Code       10       20       30       40      Page 5
----------------------------------------------------
 2917        2        0        0        0  =      2
 2916        0        0        0        5  =      5
 2915        2        0        3        0  =      5
 2914        2        2        1        0  =      5
 2912        0        2        0        0  =      2
 2911        0        2        0        0  =      2
 2910        3        0        0        0  =      3
----------------------------------------------------

2 个答案:

答案 0 :(得分:1)

这是另一个不需要任何系统命令的选项。

PsExec.exe -i -s -d \\60.60.43.3 -u MYDOMAIN\User -p 12345678 calc.exe 2>&1| find /i "ID"
If %errorlevel% equ 0 echo OK
If %errorlevel% equ 1 echo NO
Pause

$ cat a.awk
BEGIN {
    hdr1 = "----------------------------------------------------\n"
    hdr2 = "Code       10       20       30       40     Page %d\n"
    hdr3 = "----------------------------------------------------\n"
    hdr = hdr1 hdr2 hdr3
}

NR % 7 == 1 { printf hdr, ++page }1    

答案 1 :(得分:-1)

您可以编写如下脚本:

#! /usr/bin/awk -f

BEGIN {
    line = 0;
    page = 1;
    # delete old file called out.txt
    cmd = "rm -f out.txt"
    system(cmd);
    close(cmd);
}

{
    cmd = "cat header.txt | sed 's/Page 1/Page "page"/' >> \"out.txt\""  # pay attention to header.txt and to the sed command
    if (line == 0) {
        system(cmd);
        close(cmd);
    }

    print $0 >> "out.txt"
    line++;
    if (line == 7) {
        line = 0;
        page++;
    }
}

out.txt是保存输出的地方

sed命令用正确的数字替换标题中的Page 1,并在out.txt打印出带有标题的页面的正确编号

你应该像这样执行脚本:

./script.awk file

file是您需要解析和编辑的输入文件

script.awk是您必须使用chmod +x script.awk生成可执行文件的脚本。

PS:

rm块中的BEGIN命令已完成,因为在主块中我们将行追加到out.txt并且如果已存在out.txt,那么这是一个问题,因为追加操作不会删除文件的旧内容,我们会在其中添加新行。