Sorting numeric columns based on another numeric column - linux

I have the following file:
BTA Pos KLD
4 79.7011 5.7711028907
4 79.6231 5.7083918219
5 20.9112 4.5559494707
5 50.7354 4.2495580809
5 112.645 4.0936819092
6 72.8212 4.9384741047
6 18.3889 7.3631759258
I want to use AWK or bash commands to sort the second column based on the first column to have the output as follows:
4 79.6231 5.7083918219
4 79.7011 5.7711028907
5 20.9112 4.5559494707
5 50.7354 4.2495580809
5 112.645 4.0936819092
6 18.3889 7.3631759258
6 72.8212 4.9384741047

sort numerically on column one then on column two:
$ sort -nk1,1 -nk2,2 file
BTA POS KLD
4 79.6231 5.7083918219
4 79.7011 5.7711028907
5 20.9112 4.5559494707
5 50.7354 4.2495580809
5 112.645 4.0936819092
6 18.3889 7.3631759258
6 72.8212 4.9384741047

Related

How to highlight unsorted elements in a majorly sorted list in excel

Considering the following example list:
1
2
3
10
11
4
9
8
5
6
what rule would highlight the unsorted elements, so that the following list is achieved:
1
2
3
**10**
**11**
4
**9**
**8**
5
6
You could use a rule based on a formula similar to the following:
=COUNTIF(A2:$A$10,"<"&A1)>0

Formula to transpose horizontal numeric data vertical in excel

My input data in column A
1
2
3
4
5
6
7
8
9
If I want the above in Column B C D like
1 2 3
4 5 6
7 8 9
Use INDEX with some math:
=INDEX($A:$A,(ROW($A1)-1)*3+COLUMN(A$1))
Put in B1 copy over 3 columns and down 3 rows.
The *3 is the number of columns desired.

Average from different columns in shell script

I have a datafile with 10 columns as given below
ifile.txt
2 4 4 2 1 2 2 4 2 1
3 3 1 5 3 3 4 5 3 3
4 3 3 2 2 1 2 3 4 2
5 3 1 3 1 2 4 5 6 8
I want to add 11th column which will show the average of each rows along 10 columns. i.e. AVE(2 4 4 2 1 2 2 4 2 1) and so on. Though my following script is working well, but I would like to make it more simpler and short. I appreciate, in advance, for any kind help or suggestions in this regard.
awk '{for(i=1;i<=NF;i++){s+=$i;ss+=$i}m=s/NF;$(NF+1)=ss/NF;s=ss=0}1' ifile.txt
This should work
awk '{for(i=1;i<=NF;i++)x+=$i;$(NF+1)=x/NF;x=0}1' file
For each field you add the value to x in the loop.
Next you set field 11 to the sum in xdivided by the number of fields NF.
Reset x to zero for the next line.
1 equates to true and performs the default action in awk which is to print the line.
is this helping
awk '{for(i=1;i<=NF;i++)s+=$i;print $0,s/NF;s=0}' ifile.txt
or
awk '{for(i=1;i<=NF;i++)ss+=$i;$(NF+1)=ss/NF;ss=0}1' ifile.txt

find number appears more

1 2 2
4 5 4
3 3 3
0 1 0
1 2 2
4 4 3
4 5 4
4 5 4
3 4 4
3 4 4
4 4 4
3 4 3
I have above three columns in excel,how to find the number that appears more times, for example, first row is 2 (because it has two 2 and one 1)
Assuming your layout is such that the first column is A, second column is B, etc., then you could put the following in cell D1:
=IF(ISNUMBER(MODE(A1:C1)),MODE(A1:C1),0)
This also outputs 0 in case there is a tie. You can replace the 0 with whatever excel expression you want if you don't want ties to result in a 0.

Data fill in specific pattern

I am trying to fill data in MS Excel. I am given following pattern:
1 2
1
1
2 5
2 5
2
3
3 6
3
4
4
5 4
And I want my output in following format:
1 2
1 2
1 2
2 5
2 5
2 5
3 6
3 6
3 6
4
4
5 4
I tried using if(b2,b2,c1) in column 3. but that doesn't solve the problem for a=3 and a=4.
Any idea how to do this in Excel?
With sorting thus:
(the effect of which in this case is merely to move 6 up once cell) and a blank row above:
=IF(AND(A2<>A1,B2=""),"",IF(B2<>"",B2,C1))
In C2 and copied down should get the result you ask for from the data sample provided.

Resources