LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-31-2010, 02:13 AM   #1
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
Formatting rows and columns


Hi,i have a simple table some thing like this.

100 user1
101 user1
210 user3
122 user4
120 user3
111 user2
099 user2

How do i format this,such that the data presented is in this form.

user1 100 101
user3 210 120
user2 111 099
user4 122

how do i get this formatting using gawk or sed. Thanks
 
Old 08-31-2010, 02:27 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,438

Rep: Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879
Have you tried anything? Where are you stuck? I would suggest using (g)awk as it would be trivial.
 
Old 08-31-2010, 02:32 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Something like this:
Code:
#!/bin/bash

sort -k2 infile | \
awk '{ userArray[$2] = userArray[$2]" "$1 }
END { for ( item in userArray )
     print item, userArray[item] }'
Example run:
Code:
$ cat infile
100 user1
101 user1
210 user3
122 user4
120 user3
111 user2
099 user2

$ ./awk.format.rows.columns.sh
user1  100 101
user2  099 111
user3  120 210
user4  122
Hope this helps.

EDIT: Did not see grail's reply. He does have a good point, do post what you have tried and what doesn't (seem to) work.
 
1 members found this post helpful.
Old 08-31-2010, 02:39 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Code:
#!/bin/bash
#bash 4.0
declare -A array
exec 4<"file"
while read -r a b
do
  array[$b]+="$a "
done <&4
exec 4<&-

for i in ${!array[@]}
do
  echo $i ${array[$i]}
done
 
1 members found this post helpful.
Old 08-31-2010, 02:56 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,750

Rep: Reputation: 462Reputation: 462Reputation: 462Reputation: 462Reputation: 462
You might try the 'fmt' command also.
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a2ps - How to get 80 rows by 130 columns packer_fan Linux - Software 1 08-28-2010 11:06 AM
Convert columns of data into rows jaufer Linux - Software 3 06-29-2010 05:19 PM
How to print data in rows and columns suran Linux - General 3 03-15-2009 02:53 PM
columns & rows Ammad Linux - General 1 08-08-2005 04:02 AM
rows and columns digitalgravy Linux - General 2 03-16-2004 06:47 PM


All times are GMT -5. The time now is 09:49 AM.

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