代码之家  ›  专栏  ›  技术社区  ›  bapors

向右移动列标题

  •  0
  • bapors  · 技术社区  · 6 年前

    我有一个文件,我想在bash或python中处理它。 结构有4列,但只有3列标题:

    input.txt
    
    1STCOLUMN   2NDCOLUMN   THIRDCOLUMN
    input1         12             33             45
    input22        10             13              9
    input4          2             23             11
    input4534       3              1              1
    

    我正在尝试将标题列右移并添加标题 "INPUTS"

    所需输出:添加列标题

    Desired-output-step1.csv
    
        INPUTS     1STCOLUMN     2NDCOLUMN    THIRDCOLUMN
        input1          12         33            45
        input22         10         13             9
        input4           2         23            11
        input4534        3          1             1
    

    我试过了 sed

    sed -i '1iINPUTS, 1STCOLUMN, 2NDCOLUMN, THIRDCOLUMN' input.txt
    

    但由于这个原因,我不喜欢键入列的名称。

    如何将新标题插入第一列,其他列标题向右移动?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Sundeep    6 年前

    可以使用行号指定要替换的行

    $ sed '1s/^/INPUTS       /' ip.txt
    INPUTS       1STCOLUMN   2NDCOLUMN   THIRDCOLUMN
    input1         12             33             45
    input22        10             13              9
    input4          2             23             11
    input4534       3              1              1
    
    • 在这里, 1 表示要应用 s 仅用于第1行的命令
    • s/^/INPUTS / 在行首插入一些东西,你必须根据需要调整间距
        2
  •  1
  •   Kent    6 年前

    column -t 执行填充和格式化工作:

    sed '1s/^/INPUTS /' ip.txt|column -t
    

    INPUTS     1STCOLUMN  2NDCOLUMN  THIRDCOLUMN
    input1     12         33         45
    input22    10         13         9
    input4     2          23         11
    input4534  3          1          1