Counting and rearranging the rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting and rearranging the rows
# 1  
Old 02-25-2013
Counting and rearranging the rows

Hi,

I have a file that I re-arranged using awk and unix commands to produce a file that looks like this:

Code:
JOE
JOE
JOE
JOE
JOE
BOB
BOB
HI
HI
HI

I want to count how many of the same rows there are and print it on the second column while only maintaining the original name once.

The output file would look like this:

Code:
JOE  5
BOB 2
HI    3

Thanks
# 2  
Old 02-25-2013
Code:
'{arr[$0]++; next} END{for(i in arr){print i, arr[i]}} '   inputfile

# 3  
Old 02-25-2013
Code:
uniq -c filename | awk '{ print $2, $1 }'

# 4  
Old 02-26-2013
@jim mcnamara
Why do you use the next? Does the code run faster?
# 5  
Old 02-26-2013
Jotne, IMHO using next in this code will not make any difference because there are no further conditions to check and actions to be performed on input record.

Also END block is going to execute only once after all input records are read.
# 6  
Old 02-26-2013
That was my thought too. Only one condition and then next.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rearranging the column

I have a text file like this, I would like to rearrange the first column (Name) according to the third column(percentage)in descending order. I mean methionine with the highest percentage should be the first one to appear under the name column. But I also want to exclude the headers from this... (2 Replies)
Discussion started by: cathum
2 Replies

2. Shell Programming and Scripting

Data rearranging from rows to column

Hello Everyone, I have a input file looks like -0.005-0.004-0.003-0.002-0.00100.0010.0020.0030.0040.005My desired output should look like -0.005 -0.004 -0.003 -0.002 -0.001 0 0.001 0.002 0.003 0.004 0.005I had some success in getting the desired output. But i face a problem when i... (15 Replies)
Discussion started by: dinesh.n
15 Replies

3. Shell Programming and Scripting

Rearranging of values as desired

i have a file as below :- 100 D 22 100 T 33 100 C 89 101 C 55 101 D 44 102 D 88 103 T 22 103 C 13 output format :- <number> <D value> <C Value> <T Value> if no value then zero. I want output as :- 100 22 33 89 101 44 55 0 102 88 0 0 103 0 13 22 (3 Replies)
Discussion started by: satishmallidi
3 Replies

4. Shell Programming and Scripting

Rearranging into new columns (awk?)

Hi experts, I've used several solutions from this forum to delete nonsense and rearrange data in the project file I'm working on. I'm hoping you guys can give me some tips on further rearranging the data (I've seen a few solutions by searching, but one specific item has me stumped, which is only... (5 Replies)
Discussion started by: coryvp
5 Replies

5. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

6. Shell Programming and Scripting

Rearranging

Hello, I spent all day trying to write a script and cannot find the solution :( I have plenty files looking like this: several hundred lines precede the following interesting Bla xxx: Blub = -7537.37687 Blub = -100.644746 Blub = -3247.61954 . . . Blub = 1324.82567 Blub =... (2 Replies)
Discussion started by: tempestas
2 Replies

7. UNIX for Dummies Questions & Answers

Rearranging whole columns

Hello all, I have a text file that is arranged: name 3 7 2 9 5 jim a d e g k max d g u x g rob f w v k o This is just an example as my real file has >1000 individuals and >64,000 columns. I need to rearrange the file so that the columns appear in numerical order so that name... (3 Replies)
Discussion started by: doobedoo
3 Replies

8. Shell Programming and Scripting

Rearranging columns

Hi, I have an input file as follows : input.txt abcdTXXqwe axdfSYYrew dasgTXXqwt gtfsTYYwer gadfSXXerw gwerSYYTXX Now I have to get four output files. output1.txt should have the first four cloumns, Where the rows containing 5th column as T and 6th-7th columns as XX output2.txt... (5 Replies)
Discussion started by: sudhamacs
5 Replies

9. UNIX for Dummies Questions & Answers

Counting rows in many files

I'm regularly counting the number of rows in a number of files. I need to know how many rows their are in all files together. Counting rows in one file I can handle, but how do I count rows in all at once? I'd be grateful for any answer. (7 Replies)
Discussion started by: DrZoidberg
7 Replies

10. Shell Programming and Scripting

Rearranging fields from a pipe

I have a large file that I am pulling only certain fields out of but my output I would like to rearrange the field order via a pipe. I have been looking through the site and man pages and have come to a loss. I am running on HP cut -c33-38,44-46,62-65,91-98 <file> | grep -e <value> >... (4 Replies)
Discussion started by: bthomas
4 Replies
Login or Register to Ask a Question