How can I better group my output?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How can I better group my output?
# 1  
Old 02-07-2008
How can I better group my output?

I have a script that puts out a list that looks like this:

bb1 newyork
bb1 pittsburg
bb2 seattle
bb2 spokane
sl1 sacramento

I want to seperate the output so that it instead has a space between each group so like this:

bb1 newyork
bb1 pittsburg

bb2 seattle
bb2 spokane

sl1 sacramento

How can I do this?

Thanks!
# 2  
Old 02-07-2008
Code:
nawk "(FNR%2 && FNR!=1) {print RS}1" myFile

# 3  
Old 02-07-2008
Thank you for the reply.
I am new to all this. Can you break down the code you gave me so that I can understand it?

Also my list was just an example snapshot. I actually have a list that has multiple number of entries for each group.


Thanks again!
# 4  
Old 02-07-2008
Quote:
Originally Posted by llsmr777
Thank you for the reply.
I am new to all this. Can you break down the code you gave me so that I can understand it?

Also my list was just an example snapshot. I actually have a list that has multiple number of entries for each group.


Thanks again!
what does constitute a 'group'?

Currently, the script 'groups' adjacent 2 lines together - as a your sample's shown.
# 5  
Old 02-07-2008
Well in my real list the list with bb1 - could be 20 lines and bb2 - 10 and sl1 30 - instead of just what's listed below. And the numbers can change.

thanks for your reply!!!

bb1 newyork
bb1 pittsburg

bb2 seattle
bb2 spokane

sl1 sacramento
# 6  
Old 02-07-2008
Quote:
Originally Posted by llsmr777
Well in my real list the list with bb1 - could be 20 lines and bb2 - 10 and sl1 30 - instead of just what's listed below. And the numbers can change.

thanks for your reply!!!

bb1 newyork
bb1 pittsburg

bb2 seattle
bb2 spokane

sl1 sacramento
assuming your file is sorted on the FIRST column:
Code:
nawk 'f != $1 {
     if (FNR!=1) print ""
     f=$1
}
1' myFile

# 7  
Old 02-07-2008
That was great. Just a few things:

the first line did not get sorted? see how newyork is the first line instead of jsut BB4? Also how can I put a space between the last BB4 and the first BB5?

BB4 newyork
BB4
BB4 pittsburg
BB5
BB5 Buffalo
BB5 colorado
BB5 DesMoines
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Sql ORA-00937: not a single-group group function

I'm trying to return only one row with the highest value for PCT_MAX_USED. Any suggestions? When I add this code, I get the ORA-00937 error. trunc(max(decode( kbytes_max, 0, 0, (kbytes_alloc/kbytes_max)*100))) pct_max_used This is the original and returns all rows. select (select... (3 Replies)
Discussion started by: progkcp
3 Replies

2. Shell Programming and Scripting

Joining multi-line output to a single line in a group

Hi, My Oracle query is returing below o/p ---------------------------------------------------------- Ins trnas value a lkp1 x a lkp1 y b lkp1 a b lkp2 x b lkp2 y ... (7 Replies)
Discussion started by: gvk25
7 Replies

3. Shell Programming and Scripting

need a one liner to grep a group info from /etc/group and use that result to search passwd file

/etc/group tiadm::345:mk789,po312,jo343,ju454,ko453,yx879,iy345,hn453 bin::2:root,daemon sys::3:root,bin,adm adm::4:root,daemon uucp::5:root /etc/passwd mk789:x:234:1::/export/home/dummy:/bin/sh po312:x:234:1::/export/home/dummy:/bin/sh ju454:x:234:1::/export/home/dummy:/bin/sh... (6 Replies)
Discussion started by: chidori
6 Replies

4. Shell Programming and Scripting

ID - output group-memberships one per line

Hello, I am currently a little bit lost with my new shell-script. I am trying to output the results of the "id"-command as a "one per line" statement. For example: I have the user "test" with the group-memberships "group1, group2, group3". When I enter "id test" it will show me the output... (2 Replies)
Discussion started by: henryford
2 Replies

5. Shell Programming and Scripting

How to group the output w/ limit

Hi All, Second time to post on this group :) I'm pulling my hair now 'coz I'm so dumb to produce this requirement. Requirement: I want to run a utility by limiting the no. inside my process (mov##) to be able to use in multi streaming. Here is my script: --Input: "user_list.txt"... (0 Replies)
Discussion started by: alvingo
0 Replies

6. Shell Programming and Scripting

How to group the output of a loop

Hi Guys, This is based on my question previously posted. :) I have my shell script like this: #!/usr/bin/sh e_id=`sqlplus -s scott/tiger@DB<<eof SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF; select emp_id from employee; quit ... (1 Reply)
Discussion started by: alvingo
1 Replies

7. Shell Programming and Scripting

Group the output of the variable

Hi Guys, First time in scripting & first time here...:D I need help in this situation - My requirements is: I have a list e.g. list=`echo ls -l *` OR list=`sql.... select emp_id from employee ...` The Input is: ls -l OR emp_id A 4567 B 2231 C ... (2 Replies)
Discussion started by: alvingo
2 Replies

8. UNIX for Dummies Questions & Answers

output of file from several machines written to network share, then emailed to group.

I have a script on all the machines on my network that lists how many updates are available for each machine, and then outputs the answer to a file called updates.txt the output shows the hostname and the number of updates, like: computer_A 7 I want all these machines to output the data to... (1 Reply)
Discussion started by: glev2005
1 Replies

9. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies
Login or Register to Ask a Question