Rearrange columns using cut

后端 未结 8 1522
难免孤独
难免孤独 2020-11-28 03:53

I am having a file in the following format

Column1    Column2
str1       1
str2       2
str3       3

I want the columns to be rearranged. I tried below

8条回答
  •  清歌不尽
    2020-11-28 04:27

    Just been working on something very similar, I am not an expert but I thought I would share the commands I have used. I had a multi column csv which I only required 4 columns out of and then I needed to reorder them.

    My file was pipe '|' delimited but that can be swapped out.

    LC_ALL=C cut -d$'|' -f1,2,3,8,10 ./file/location.txt | sed -E "s/(.*)\|(.*)\|(.*)\|(.*)\|(.*)/\3\|\5\|\1\|\2\|\4/" > ./newcsv.csv
    

    Admittedly it is really rough and ready but it can be tweaked to suit!

提交回复
热议问题