Visit Our UNIX and Linux User Community


Sorting data


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sorting data
# 1  
Old 03-24-2010
Sorting data

Hello guys. I need help figuring this one out. It's probably really easy. Thanks in advance!

I have a file say for example containing this:

Rice Food
Carrots Food
Beans Food
Plates Kitchen
Fork Kitchen
Knives Kitchen



I need:

Food Rice, Carrots, Beans
Kitchen Plates, Fork, Knives



I also don't mind if I get the following if it makes it easier:

Food, Rice, Carrots, Beans
Kitchen, Plates, Fork, Knives
# 2  
Old 03-24-2010
What have you tried so far, where are you stuck? Because this seems almost like homework.
# 3  
Old 03-24-2010
Please be more specific and use code tags. Thanks!

Are you saying that you have a list like this:

Code:
File:
Rice Food
Carrots Food
Beans Food
Plates Kitchen
Fork Kitchen
Knives Kitchen

And that each line of the file contains two words. This second word is the category, and the first word is the data in that category. ie,

Code:
Food:
   Rice
   Carrots
   Beans

Kitchen:
   Plates
   Forks
   Knifes


If that is true, it is relatively trivial.

Code:
$ cat dat.txt
Rice Food
Carrots Food
Beans Food
Plates Kitchen
Fork Kitchen
Knives Kitchen

#!/usr/bin/perl 

use strict;
use warnings;

my %list;
my $key;
my $val;

while (<>) {
	if (/^(\w*) (\w*)$/) {
		$key=$2; 
		$val=$1;
		push @{ $list{ $key } }, $val; 
	}	
}	

foreach $key (sort keys %list) { 
	print "$key:\n"; 
	foreach (sort @{ $list{$key} } ) { 
		print "\t$_\n"; 
	} 
} 

$ thatperlsc.pl dat.txt
Food:
	Beans
	Carrots
	Rice
Kitchen:
	Fork
	Knives
	Plates

# 4  
Old 03-24-2010
I'm doing this for work. I'm trying to sort a bunch of data (can't post the real data) that is in different categories. I tried using
Quote:
grep -w Kitchen | awk '{print $1}' | tr '\n' ' '
but I can't get the Kitchen to show up in front. Also the data in the categories could be bigger or lesser than 3.

Thanks for the reply drewk. I know I should have posted this in the shell section but I didn't realized it until I posted it. Is there anyway for this to be done in bash? It's going to be incorporated into a bigger script.

Last edited by visuelz; 03-24-2010 at 05:05 PM..
# 5  
Old 03-24-2010
Shortest I could come up with
Code:
#!/usr/bin/perl -W

use strict;

my %places;
while ( my $line = <DATA> ) {
    chomp $line;
    my @parts = split /\s/, $line;
    push @{ $places{ $parts[1] } }, $parts[0];
}

foreach my $place (sort keys %places) {
    print $place, " ", join( ", ", sort @{ $places{$place} } ), "\n";
}

__DATA__
Rice Food
Carrots Food
Beans Food
Plates Kitchen
Fork Kitchen
Knives Kitchen

# 6  
Old 03-24-2010
Well if you really want to use awk:

Code:
$ cat dat.txt
Rice Food
Carrots Food
Beans Food
Plates Kitchen
Fork Kitchen
Knives Kitchen
$ awk -v pat="Food" 'BEGIN { printf pat", " } $2 ~ pat { printf $1", " }'  dat.txt | sed 's/, $//g'
Food, Rice, Carrots, Beans
$
$ awk -v pat="Kitchen" 'BEGIN { printf pat", " } $2 ~ pat { printf $1", " }'  dat.txt | sed 's/, $//g'
Kitchen, Plates, Fork, Knives

# 7  
Old 03-25-2010
A shell "open logic" approach:

Code:
#!/bin/ksh
FIRST_TIME="Y"
CATEGORY_PREV="XXXXX"
ITEM_LIST=""
#
awk '{print $2,$1}' filename|while read CATEGORY ITEM
do
        # First time through?
        if [ "${FIRST_TIME}" = "Y" ]
        then
                ITEM_LIST="${ITEM}"
                CATEGORY_PREV="${CATEGORY}"
                FIRST_TIME="N"
                continue
        fi
        #
        # Same category?
        if [ "${CATEGORY}" = "${CATEGORY_PREV}" ]
        then
                ITEM_LIST="${ITEM_LIST}, ${ITEM}"
                continue
        fi
        #
        # New category
        echo "${CATEGORY_PREV} ${ITEM_LIST}"
        CATEGORY_PREV="${CATEGORY}"
        ITEM_LIST="${ITEM}"
done
#
# Output last line
if [ ! "${CATEGORY_PREV}" = "XXXXX" ]
then
        echo "${CATEGORY_PREV} ${ITEM_LIST}"
fi




Food Rice, Carrots, Beans
Kitchen Plates, Fork, Knives

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #539
Difficulty: Medium
In C, int x; creates an integer variable associated with the identifier x initialized to one .
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How AS 400 sorting data?

Hi Gurus, I have a requests to sort data based on AS 400 sorting order. below is example: the data is sorted by ascending order. could anybody explain how AS 400 sort data? IMM00007 07918607 1242 423 (3 Replies)
Discussion started by: green_k
3 Replies

2. Shell Programming and Scripting

Sorting the data with date

Hi, PFB the data: C_Random_130417 Java_Random_130518 Perl_Random_120519 Perl_Random_120528 so the values are ending with year,i.e.,130417 i want to sort the values with date. i want the output like this: Perl_Random_120519 Perl_Random_120528 C_Random_130417 Java_Random_130518 can... (5 Replies)
Discussion started by: arindam guha
5 Replies

3. Shell Programming and Scripting

Sorting the Data

My actual data looks like below i have given only format. i can't give exact data format of my requirement due to some reasons. I this set of data lines about 5000 I need to come up with information in below exact format of my data set : Line<space>Number1<space>"somedata":... (1 Reply)
Discussion started by: ckaramsetty
1 Replies

4. UNIX for Dummies Questions & Answers

Help with Data Sorting

Hi All, I have a long list made of 4 columns containing entries such as the following example: a b c d 0 0 0 0 1 2 1 2 2 5 3 4 3 8 4 6 4 10 9 8 5 15 8 10So the top row is the header and I need to arrange the data in a way as to... (11 Replies)
Discussion started by: pawannoel
11 Replies

5. UNIX for Dummies Questions & Answers

Help with Data Sorting Command

Hi, I have a problem on data sorting, example my file as below: 123 123/789 aaa bbb ccc ddd (adf) 112 112/123 aaa bbb ccc (ade) 102 1a3/7g9 (adf)03 110 12b/129 aaa bbb ccc ddd fff(a8f)03 117 42f/8c9 aaa bbb ccc ddd (adf) 142 120/tyu fff... (7 Replies)
Discussion started by: 793589
7 Replies

6. UNIX for Dummies Questions & Answers

Sorting data from a to z

Hi, Let's say I have these 3 columns; NGC1234 6 9 SL899 4 1 NGC1075 8 3 SL709 5 2 And I want to sort the data according to the first column (from a to z) like having them as: NGC1075 8 3 NGC1234 6 9 SL709 5 2 SL899 4 1 Can that be done... (2 Replies)
Discussion started by: cosmologist
2 Replies

7. Shell Programming and Scripting

PERL data - sorting

Hello, I have a page where multiple fields and their values are displayed. But I am able to sort only a few fields. When I looked into the issue, it is seen that the for each row of info , an unique id is generated and id.txt is generated and saved. Only those fields which are inside that id.txt... (3 Replies)
Discussion started by: eagercyber
3 Replies

8. UNIX for Dummies Questions & Answers

sorting data from who by IP

Hello. I have an RS/6000 running AIX 4 and I need to be able to see if there are any users that are logged on more than once from the same terminal so I can kick them off to make room for other terminals. 64 connections is the limit. Currently I am doing this: who | more and then manually... (11 Replies)
Discussion started by: raidzero
11 Replies

9. Shell Programming and Scripting

Sorting blocks of data

Hello all, Below is what I am trying to accomplish: I have a file that looks like this /* ----------------- xxxx.y_abcd_00000050 ----------------- */ jdghjghkla sadgsdags asdgsdgasd asdgsagasdg /* ----------------- xxxx.y_abcd_00000055 ----------------- */ sdgsdg sdgxcvzxcbv... (8 Replies)
Discussion started by: alfredo123
8 Replies

Featured Tech Videos