04-08-2010
How to sort by count
Hello!
I have a file with 4 columns. I am trying to have it sort first with respect to the first column, and then with respect to the number of counts (in descending count) in the second column within the same first column identity.
For example:
Input:
1 2 A 1
1 6 B 2
2 5 G 7
1 6 D 4
1 6 F 6
1 0 E 5
2 4 H 8
2 5 I 9
1 2 C 3
The output is to look like:
1 6 B 2
1 6 D 4
1 6 F 6
1 2 A 1
1 2 C 3
1 0 E 5
2 5 G 7
2 5 I 9
2 4 H 8
As you can see:
-Within the first column identity=1, there are 3 counts of '6', 2 counts of '2' and 1 count of '0' in the second column.
-Within the first column identity=2, there are 2 counts of '5' and 1 count of '4' in the second column.
They are both sorted with respect to descending count in the second column, not in arithmetic order.
I have been doing this in excel and it became quite time consuming for me since the number of files to process has started to increase.
Thanks for your help in advance!
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi,
I've a fixed width file where I need to count the number of patterns from each line between characters 1 to 15 . so can we sort them and get a count for each pattern on the file between 1 to 15 characters.
65795648617522383763831552 410828003265795648 6175223837... (5 Replies)
Discussion started by: rudoraj
5 Replies
2. UNIX for Dummies Questions & Answers
Hello!
I have a file with 4 columns. I am trying to have it sort first with respect to the first column, and then with respect to the number of counts (in descending count) in the second column within the same first column identity.
For example:
Input:
1 2 A 1
1 6 B 2
2 5 G 7
1 6 D 4... (8 Replies)
Discussion started by: anchuz
8 Replies
3. Shell Programming and Scripting
I want to sort lines by how many times a string occurs in each line (the most times first).
I know how to do this in two passes (add a count field in the first pass then sort on it in the second pass).
However, can it be done more optimally with a single AWK command? My AWK has improved... (11 Replies)
Discussion started by: Michael Stora
11 Replies
4. Shell Programming and Scripting
Hi,
I'm new to linux & bash so please forgive my ignorance, just wondering if anyone can help.
I have a file (mainfile.txt) with comma deliminated values, like so:
$1 $2 $3
613212, 36, 57
613212, 36, 10
613212, 36, 10
677774, 36, 57
619900, 10, 10
i need to split this file... (12 Replies)
Discussion started by: BigTOE
12 Replies
5. Shell Programming and Scripting
Can any one please help I am trying to sort each item in every line and count the common (non case sensitive) and at the end printing all the unique alphabetically.
Here is what i did ... I can print all the lines but struck to sort each line some were:
I am getting the output as:
I... (5 Replies)
Discussion started by: sureshcisco
5 Replies
6. UNIX for Advanced & Expert Users
Hello all -
I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies
7. Shell Programming and Scripting
I am trying to count a special character in several thousand files and sort the result according to the number the character occured in each files.
files:
1.txt
2.txt
3.txt
files look like:
ABCDEFGHABBBACF
I want the number of B in each file and a result file that lists the occurence... (4 Replies)
Discussion started by: patdoor
4 Replies
8. Shell Programming and Scripting
Hi. I am not sure the title gives an optimal description of what I want to do. Also, I tried to post this in the "UNIX for Dummies Questions & Answers", but it seems no-one was able to help out.
I have several text files that contain data in many columns. All the files are organized the same... (14 Replies)
Discussion started by: JamesT
14 Replies
9. Shell Programming and Scripting
Hi Experts,
I have a filelist collected from another server , now want to sort the output using date/time stamp filed.
- Filed 6, 7,8 are showing the date/time/stamp.
Here is the input:
#----------------------------------------------------------------------
-rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies
10. UNIX for Advanced & Expert Users
Hi Folks :)
I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line.
Example
from:
word
word word word
word word
word word word word
word
word word word
word word
to desired output:
word (2 Replies)
Discussion started by: martinsmith
2 Replies
LEARN ABOUT NETBSD
column
COLUMN(1) BSD General Commands Manual COLUMN(1)
NAME
column -- columnate lists
SYNOPSIS
column [-tx] [-c columns] [-s sep] [file ...]
DESCRIPTION
The column utility formats its input into multiple columns. Rows are filled before columns. Input is taken from file operands, or, by
default, from the standard input. Empty lines are ignored.
The options are as follows:
-c Output is formatted for a display columns wide.
-s Specify a set of characters to be used to delimit columns for the -t option.
-t Determine the number of columns the input contains and create a table. Columns are delimited with whitespace, by default, or with
the characters supplied using the -s option. Useful for pretty-printing displays.
-x Fill columns before filling rows.
column exits 0 on success, >0 if an error occurred.
ENVIRONMENT
COLUMNS The environment variable COLUMNS is used to determine the size of the screen if no other information is available.
EXAMPLES
(echo "PERM LINKS OWNER GROUP SIZE MONTH DAY HH:MM/YEAR NAME";
ls -l | sed 1d) | column -t
SEE ALSO
colrm(1), ls(1), paste(1), sort(1)
HISTORY
The column command appeared in 4.3BSD-Reno.
BSD
March 9, 2008 BSD