LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-12-2014, 10:34 PM   #16
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660

To improve "understandability" I offer another solution which uses one-dimensional arrays. This version simplifies the solution by piping two awks. The first one transposes the matrix; the second one generates the desired result.

With this InFile ...
Code:
A1 A2 A3 A4
B1 B2 B3 B4 B5
C1 C2 C3
A1 B2 C3 A4
... these piped awks ...
Code:
 awk  '{for (j=1;j<=NF;j++) a[j]=a[j]" "$j} 
       END {j=1; while (j in a) {print a[j]; j++}}' $InFile  \
|awk '{{for (j=1;j<=NF;j++) {a[$j]++}; n=asorti(a,b);
        for (j=1;j<=n;j++) print b[j],":",a[b[j]]}
  print "==============="; delete a}' >$OutFile
... produced this OutFile ...
Code:
A1 : 2
B1 : 1
C1 : 1
===============
A2 : 1
B2 : 2
C2 : 1
===============
A3 : 1
B3 : 1
C3 : 2
===============
A4 : 2
B4 : 1
===============
B5 : 1
===============
Daniel B. Martin

Last edited by danielbmartin; 02-12-2014 at 10:36 PM. Reason: Correct grammar
 
Old 02-12-2014, 11:14 PM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Code:
END {
	for (j=1; j<=length(counter); j++){
		for (i in counter[j]) {
			print counter[j][i], ":",i; 
			}
		print "========================"
	}
}
2 things:

1. Correction provided as the 'i' variable is already the 'label'

2. Using the for(i in array) process does not guarantee order. When it does come out correctly you have got lucky
 
Old 02-12-2014, 11:48 PM   #18
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
All of you are obviously masters of awk. However, for an awk newbie like me, the 2-dimensional solution is much clearer.

I intend to put the combined output in an array, sort it and then print it, as was suggested earlier.

Thanks for your help.

Last edited by rng; 02-12-2014 at 11:50 PM.
 
  


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
Could someone please explain the concept of associative arrays in AWK programming? AJAY E Linux - Newbie 6 05-27-2012 07:01 PM
[SOLVED] Associative array in bash not unsetting grail Programming 4 04-07-2011 09:11 AM
Sorting through associative array with user input nobody123 Linux - Newbie 3 04-08-2009 01:55 PM
awk: associative array key names h/w Programming 2 10-17-2007 12:58 AM
exchange value and key per element in associative array rblampain Programming 3 04-02-2006 09:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:33 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration