Search code examples
linuxawksedpastecut

Linux cut, paste


I have to write a script file to cut the following column and paste it the end of the same row in a new .arff file. I guess the file type doesn't matter.

Current file:

63,male,typ_angina,145,233,t,left_vent_hyper,150,no,2.3,down,0,fixed_defect,'<50'
67,male,asympt,160,286,f,left_vent_hyper,108,yes,1.5,flat,3,normal,'>50_1'

The output should be:

male,typ_angina,145,233,t,left_vent_hyper,150,no,2.3,down,0,fixed_defect,'<50',63
male,asympt,160,286,f,left_vent_hyper,108,yes,1.5,flat,3,normal,'>50_1',67

how can I do this? using a Linux script file?


Solution

  • sed -r 's/^([^,]*),(.*)$/\2,\1/' Input_file
    

    Brief explanation,

    • ^([^,]*) would match the first field which separated by commas, and \1 behind refer to the match
    • (.*)$ would be the remainding part except the first comma, and \2 would refer to the match