我有一个文件,我想用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
但是由于这个原因,我不喜欢输入列名。
如何将新标题插入第一列,而其他列标题移至右侧?
答案 0 :(得分:1)
您可以使用行号指定要替换的行
$ 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 /
在行首插入内容,您必须根据需要调整间距答案 1 :(得分:1)
您可以让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