I have to ascend the number of two selected columns by horizontal manner.
For example, I have a data like this in csv file (tab delimited format)
08 1 19185 18010
16 4 7960 9339
01 6 516769 517428
09 9 51384 49270
I need to ascend the two columns numbers (horizontal manner) like as follows,
08 1 18010 19185
16 4 7960 9339
01 6 516769 517428
09 9 49270 51384
Is it possible to do by command? I need to change the order like this for multiple files. Therefore, please help me to do the same.
Do you need to have all of the last two columns changed if they are not in ascending order? This code does that. All rows could be affected, depending on the data.
awk ' $4 > $5 {printf( "%s\t%s\t%s\t%s\t%s\n", $1, $2, $3, $5, $4); next}
{print $0}' file.csv > tmpfile.csv
Please note, we are not a coding service. What I provided is the code to change order of the last two columns.
1 Like
Thank you so much jim. I tried your commands, but its not working.
drl
April 6, 2019, 10:47am
4
Hi.
Assuming sample data is on file z5; code is on file z6: changing Jim's code to this:
awk ' $3 > $4 {printf( "%s\t%s\t%s\t%s\n", $1, $2, $4, $3); next}
{print $0}' z5
produces this output from your sample input:
$ ./z6
08 1 18010 19185
16 4 7960 9339
01 6 516769 517428
09 9 49270 51384
As Jim says, we're not a coding service, so we will expect you to:
1) be far more explicit, does not work / its not working tells us essentially nothing, clearly it did work, it simply was not what you were looking for,
2) put in more of your effort in the future.
Best wishes ... cheers, drl
4 Likes
Thank you drl,
Thank you for your valuable help and suggestions. I assure you that, hereafter I will put my effort to create codes.
1 Like
RudiC
April 8, 2019, 2:45am
6
Maybe somewhat simpler? Try
$3 > $4 {TMP = $3; $3 = $4; $4 = TMP; } 1' OFS="\t" file