Search code examples
linuxsedtitleshift

Shifting column titles to right


I have a file which I want to process it in bash or python. The structure is with 4 columns but only with 3 column titles:

input.txt

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

I am trying to shift the title columns to right and add a title of "INPUTS" to the first column (input column).

Desired output: Adding the column title

Desired-output-step1.csv

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

I tried with sed:

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

But I do not prefer to type the names of the columns for this reason.

How do I just insert the new title to first column and the other column titles shift to right?


Solution

  • you can specify which line to be replaced using line numbers

    $ 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
    
    • here, 1 indicates that you want to apply s command only for 1st line
    • s/^/INPUTS / insert something to start of line, you'll have to adjust the spacing as needed