Sorting a specific column!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting a specific column!
# 1  
Old 07-27-2015
Display Sorting a specific column!

What im trying to do is sort the output by the number on the second column and than limit the result to only the first three lines.

This is the code

Code:
idlist="x23s52; f34233; 2343xs; 25x34; si342d"
cntr=1
idcnt=$(print $nidlist |tr ';' '\n' |wc -l)
numofgrps=0
while (($cntr <= $idcnt))
do
    idlist=$idlist\;
    numofgrps=$numofgrps\;
    currid=$(print $idlist |awk -F ';' '{print  $1}')
    numofgrps=`cat /home/n5ddc8/BI_Ab_Intito_dist/BI_Ab_Initio_dist.report.dat | grep $currnid | wc -l`
    currcnt="$currid$numofgrps \n"
    toplst=$toplst   $currcnt
    idlist=${idlist#*\;}
    cntr=$(($cntr + 1))
done
print $toplst

The output of this code looks something like this.

Code:
x23s52   3
f34233   2
21jh43   1
ad34nj   5

# 2  
Old 07-27-2015
Ascending
Code:
output | sort -k2 -n | head -n3

Descending
Code:
output | sort -k2 -rn | head -n3

Take a look at this:
Code:
numofgrps=`cat /home/n5ddc8/BI_Ab_Intito_dist/BI_Ab_Initio_dist.report.dat | grep $currnid | wc -l`

We can remove cat since grep can read from file.
Code:
grep $currnid /home/n5ddc8/BI_Ab_Intito_dist/BI_AB_Initio_dist.report.dat | wc -l

But also, grep can do the line count that wc -l does

Code:
grep -c $currnid /home/n5ddc8/BI_Ab_Intito_dist/BI_AB_Initio_dist.report.dat

By leveraging the functionality of just one program your code is more efficient.

See if you can do the same in other areas.
# 3  
Old 07-27-2015
And grep -c is even more portable, because wc -l has leading spaces on *some* OS.
Likewise grep -c ^ file is perhaps less readable but portable, compared to wc -l < file.
# 4  
Old 07-27-2015
On top of several syntactical/orthographical errors ($nidlist, $currnid, ), the entire script is way too complicated. Unless there are requirements or conditions not shown, a for loop would handle the situation much better:
Code:
for currid in "$idlist"
  do printf "%s  %d"  $currid $(grep -c $currid path/to/file)
  done | sort -k2 | head -n3

This would have the drawback that it greps through the file n times. Doing the whole thing in e.g. awk could reduce that resource consumption by far.

---------- Post updated at 22:11 ---------- Previous update was at 21:55 ----------

Would this (as a first step, and assuming NO spaces in idlist) satisfy your needs:
Code:
grep -Eo "${idlist//;/|}" file | sort | uniq -c
     5 f34233
     6 x23s52

?

Last edited by RudiC; 07-27-2015 at 05:09 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

3. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

4. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

5. Shell Programming and Scripting

[Solved] Sorting a column based on another column

hello, I have a file as follows: F0100010 A C F0100040 A G BTA-28763-no-rs 77.2692 F0100020 A G F0100030 A T BTA-29334-no-rs 11.4989 F0100030 A T F0100020 A G BTA-29515-no-rs 127.006 F0100040 A G F0100010 A C BTA-29644-no-rs 7.29827 F0100050 A... (9 Replies)
Discussion started by: Homa
9 Replies

6. Shell Programming and Scripting

Count specific characters at specific column positions

Hi all, I need help. I have an input text file (input.txt) like this: 21 GTGCAACACCGTCTTGAGAGG 50 21 GACCGAGACAGAATGAAAATC 73 21 CGGGTCTGTAGTAGCAAACGC 108 21 CGAAAAATGAACCCCTTTATC 220 21 CGTGATCCTGTTGAAGGGTCG 259 Now I need to count A/T/G/C numbers at each character location in column... (2 Replies)
Discussion started by: thienxho
2 Replies

7. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

8. Shell Programming and Scripting

Sorting multi-column values from a specific file

Hi, all. I need a shell script which gathers data from a remote XML file and then displays it according to my needs.. I need this for my job due to the fact that I need to keep track price changes of euro, usd, gold, etc. The XML file I am talking about is located at this page: cnnturk dot... (4 Replies)
Discussion started by: canimsin
4 Replies

9. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

10. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies
Login or Register to Ask a Question