LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-30-2012, 11:21 AM   #1
circus78
Member
 
Registered: Dec 2011
Posts: 121

Rep: Reputation: Disabled
Help for bash script


Hi,
I've a very long text file with this kind of content:

Quote:
aaaaaaaaa
bbbbbbbbb
ccccccccc
bbbbbbbbb
bbbbbbbbb
aaaaaaaaa
aaaaaaaaa
aaaaaaaaa
..
..
Obviously this can be very easily sorted.
My goal is to obtain a "count" for every code.
For example, if "aaaaaaaaa" appears 576 time, "bbbbbbbbb" 78, "ccccccccc" 921 and so on:

Quote:
aaaaaaaaa,576
bbbbbbbbb,78
ccccccccc,921
...
...
How can I do this?
Thankyou!
 
Old 12-30-2012, 11:53 AM   #2
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Slack14_64_Multilib
Posts: 3,081
Blog Entries: 4

Rep: Reputation: 745Reputation: 745Reputation: 745Reputation: 745Reputation: 745Reputation: 745Reputation: 745
Code:
man uniq
 
Old 12-31-2012, 03:57 PM   #3
User\ Name=`echo $USER`
LQ Newbie
 
Registered: Oct 2012
Location: Oklahoma
Distribution: Red Hat, Ubuntu, Mac OS X
Posts: 25

Rep: Reputation: 4
Try this:
Code:
for i in `cat test.txt | sort -u`; do                                            
  count=`grep ${i} test.txt | wc -l`;                                            
  echo "${i},${count}"                                                             
done
Just replace test.txt with the name of your text file. This will give you the exact output you put in your OP.

Hope this helps.

Last edited by User\ Name=`echo $USER`; 12-31-2012 at 04:04 PM. Reason: Simplified code.
 
Old 01-01-2013, 03:29 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
If you use Bash 4.0+, it's actually just simple:
Code:
#!/bin/bash

declare -A INSTANCES

while read LINE; do
	(( INSTANCES[$LINE]++ ))
done

for KEY in "${!INSTANCES[@]}"; do
	echo "$KEY,${INSTANCES[$KEY]}"
done
Code:
# bash script.sh < input_file
 
Old 01-02-2013, 02:39 AM   #5
kooru
Senior Member
 
Registered: Sep 2012
Location: Italy
Distribution: Slackware, NetBSD
Posts: 1,264
Blog Entries: 5

Rep: Reputation: 259Reputation: 259Reputation: 259
Code:
sort yourfile | uniq -c
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 12:05 AM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM


All times are GMT -5. The time now is 12:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration