gpt4 book ai didi

linux - 向右移动列标题

转载 作者:太空宇宙 更新时间:2023-11-04 09:12:51 25 4
gpt4 key购买 nike

我有一个文件,我想用 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/ 在行首插入一些内容,您必须根据需要调整间距

关于linux - 向右移动列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52780425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com