向右移动列标题

Shifting column titles to right

我有一个文件,我想在 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

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

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

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

$ 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 命令
  • s/^/INPUTS / 在行首插入内容,您必须根据需要调整间距

您可以让 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