Is it possible to ascend a numbers based on selected columns?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Is it possible to ascend a numbers based on selected columns?
# 1  
Old 04-06-2019
Is it possible to ascend a numbers based on selected columns?

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)
Code:
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,
Code:
08	1	18010	19185	
16	4	7960 	9339	
01	6	516769	517428	
09	9	49270	51384

Moderator's Comments:
Mod Comment changed colors. -- please use code tags for data

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.

Last edited by jim mcnamara; 04-06-2019 at 10:06 AM..
# 2  
Old 04-06-2019
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.
Code:
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.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-06-2019
Thank you so much jim. I tried your commands, but its not working.
# 4  
Old 04-06-2019
Hi.

Assuming sample data is on file z5; code is on file z6: changing Jim's code to this:
Code:
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:
Code:
$ ./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

Last edited by drl; 04-06-2019 at 11:55 AM..
These 4 Users Gave Thanks to drl For This Post:
# 5  
Old 04-08-2019
Thank you drl,
Thank you for your valuable help and suggestions. I assure you that, hereafter I will put my effort to create codes.
This User Gave Thanks to dineshkumarsrk For This Post:
# 6  
Old 04-08-2019
Maybe somewhat simpler? Try
Code:
$3 > $4 {TMP = $3; $3 = $4; $4 = TMP; } 1' OFS="\t" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match value in two files and replace values in selected columns

The purpose is to check if values for column 3 and 4 in file1 match with column 1 in file2. If any value match do: 1) Replace values in file2 for column 2 and 3 using the information of file1 columns 5 and 6 2) Replace string ($1,1,5) and string ($1,6,5) in file2 with values of columns 7... (8 Replies)
Discussion started by: jiam912
8 Replies

2. Shell Programming and Scripting

Please Help!!!! Awk for summing columns based on selected column value

a,b,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,bb,cc,dd,ee,ff,gg,hh,ii a thru ii are digits and strings.... The awk needed....if coloumn 9 == i (coloumn 9 is string ), output the sum of x's(coloumn 22 ) in all records and sum of y's (coloumn 23 ) in all records in a file (records.txt).... (6 Replies)
Discussion started by: BrownBob
6 Replies

3. Shell Programming and Scripting

transpose selected columns

Can I transform input like the below ? Note: Insert zeros if there is no value to transform. Input key name score key1 abc 10 key2 abc 20 key1 xxx 100 key2 xxx 20 key1 zzz 0 key2 zzz 29 key3 zzz 129 key1 yyy 39output abc ... (1 Reply)
Discussion started by: quincyjones
1 Replies

4. Shell Programming and Scripting

compare two files, selected columns only

hi! i have two files that looks like this file 1: ABS 123 456 BCDG 124 542 FGD 459 762 file 2: ABS 132 456 FGD 459 762 output would be: from file1: ABS 132 456 BCDG 124 542 from file 2: ABS 132 456 (4 Replies)
Discussion started by: kingpeejay
4 Replies

5. Shell Programming and Scripting

copying selected records from two columns to another file

Hey guys I have got a tab-separated file and I want to copy only selected records from two columns at a time satisfying specified condition, and create a new file. My tab separated file is like this ID score ID score ID Score ID score ID score 1_11 0.80 2_23 0.74 2.36 0.78 2_34 0.75 A_34... (9 Replies)
Discussion started by: jacks
9 Replies

6. Shell Programming and Scripting

Compare selected columns of two files and print whole line with mismatch

hi! i researched about comparing two columns here and got an answer. but after examining my two files, i found out that the first columns of the two files are not unique with each other. all i want to compare is the 2nd and 3rd column. FILE 1: ABS 456 315 EBS 923 163 JYQ3 654 237 FILE 2:... (1 Reply)
Discussion started by: engr.jay
1 Replies

7. Shell Programming and Scripting

Need to display the output of ls -l selected columns

Hello Friends, Hope you are doing well. I am writing a shell script to find out the log file which are not updated in last 1 hours. I've almost completed the script but need your help in formatting its outputs. Currently, the output of the script is like this(as a flat row): ... (3 Replies)
Discussion started by: singh.chandan18
3 Replies

8. UNIX for Dummies Questions & Answers

Getting rid of selected columns

Hi All, I've got a file like this: a 1 0 0 0 1 0 0 1 1 3 3 1 4 4 4 b 1 0 0 0 1 4 4 1 3 1 1 4 4 2 2 c 1 0 0 0 2 0 0 3 3 1 3 1 1 2 4 d 1 0 0 0 2 0 0 1 1 0 0 4 4 2 4 The file has ~4200 entries. I need to exclude those columns that are zeros for all those rows that have 2 in column 6. For... (0 Replies)
Discussion started by: zajtat
0 Replies

9. Shell Programming and Scripting

Compare selected columns from a file and print difference

I have learned file comparison from my previous post here. Then, it is comparing the whole line. Now, i have a new problem. I have two files with 3 columns separated with a "|". What i want to do is to compare the second and third column of file 1, and the second and third column of file 2. And... (4 Replies)
Discussion started by: kingpeejay
4 Replies

10. UNIX for Dummies Questions & Answers

selected columns exctract

Hi all, I have 50 columns are there, want to exctract 9 columns only.These are not there in sequence. How can i do it. (2 Replies)
Discussion started by: ashokkumar83
2 Replies
Login or Register to Ask a Question