Need help putting output on one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help putting output on one line
# 1  
Old 04-23-2012
Need help putting output on one line

Good afternoon,

I have been searching the web, and these forums for help. I will try my best to explain the issue, and what my desired results are.

I am doing queries in MYSQL, and need the output to be sent to a file. That file needs to have things with the same ID on the same line. To give you an idea, if I query the names of drinks, it may come out like this:
Code:
type,name
juice,orange
juice,apple
pop,coca cola
pop,pepsi

How would I get the output to be like this:
Code:
juice,orange,apple
pop,coca cola,pepsi

Here is some sample output from the script I am working on:

sample output:
Code:
9993    10.10.50.0/24    blockAdminName    Username One
9993    10.10.50.0/24    block_name    Servers
9993    10.10.50.0/24    blockAdminEmail    username.one@people.com
9993    10.10.50.0/24    blockAdminPhone    312-555-1212
9993    10.10.50.0/24    blockAdminId    1234
9993    10.10.50.0/24    blockSecName    Username Two
9993    10.10.50.0/24    blockSecEmail    username.two@people.com
9993    10.10.50.0/24    blockSecPhone    312-555-2121
9993    10.10.50.0/24    blockSecId    7891
9993    10.10.50.0/24    blockTechName    Username Three
9993    10.10.50.0/24    blockTechEmail    username.three@people.com
9993    10.10.50.0/24    blockTechPhone    312-555-1313
9993    10.10.50.0/24    blockTechId    4567
9992    10.10.40.0/24    block_name    unknown
9992    10.10.40.0/24    blockAdminName    Username One
9992    10.10.40.0/24    blockAdminEmail    username.one@people.com
9992    10.10.40.0/24    blockAdminPhone    312-555-1212
9992    10.10.40.0/24    blockAdminId    1234
9992    10.10.40.0/24    blockSecName    Username Two
9992    10.10.40.0/24    blockSecEmail    username.two@people.com
9992    10.10.40.0/24    blockSecPhone    312-555-2121
9992    10.10.40.0/24    blockSecId    7891
9992    10.10.40.0/24    blockTechName    Username Three
9992    10.10.40.0/24    blockTechEmail    username.three@people.com
9992    10.10.40.0/24    blockTechPhone    312-555-1313
9992    10.10.40.0/24    blockTechId    4567
9991    10.10.30.0/24    block_name    unknown
9991    10.10.30.0/24    blockAdminName    Username One
9991    10.10.30.0/24    blockAdminEmail    username.one@people.com
9991    10.10.30.0/24    blockAdminPhone    312-555-1212
9991    10.10.30.0/24    blockAdminId    1234
9991    10.10.30.0/24    blockSecName    Username Two
9991    10.10.30.0/24    blockSecEmail    username.two@people.com


How can I get this to put everthing on three lines? Notice that there are only three different ID's (9991, 9992, 9993). Ideally, I would want something like this:

Code:
9992,10.10.50.0/24,blockAdminName="Username One",block_name="Servers",blockAdminEmail="username.one@people.com",blockAdminPhone="312-555-1212",blockAdminId="1234",blockSecName="Username Two",blockSecEmail="username.two@people.com",blockSecPhone="312-555-2121",blockSecId="7891",blockTechName="Username Three",blockTechEmail="username.three@people.com",blockTechPhone="312-555-1313",blockTechId="4567"

Notice that everything is on one line now. The first two columns are the same (9993 and 10.10.50.0/24). So it kept those and then put everything after that comma delimmited.

How can I accomplish this in Perl or Bash?

Thank you very much!!!!!

Moderator's Comments:
Mod Comment Please use [code] tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-23-2012 at 05:53 PM..
# 2  
Old 04-23-2012
Hi, see if this works:
Code:
awk 'p!=$1{if(p)print RS; print p=$1,$2}{print OFS $3 "=\"" $4 "\""} END{print RS}' OFS=, ORS= infile

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-23-2012
Might need something like this for the *Name fields:

Code:
awk 'p!=$1{print (p?RS:"")(p=$1),$2;} {for(i=4;i<=NF;i++)printf(i>4?" ":OFS $3 "=\"") $i; print "\"" } END{if(p)print RS}' OFS=, ORS= infile

These 2 Users Gave Thanks to Chubler_XL For This Post:
# 4  
Old 04-24-2012
When I run this, it doesn't get rid of the extra lines and condense them onto one line.

It does get rid of the first two fields on all lines except for line 1. How can I take this and put them all on the first line seperated by commas?

Here is what it looks like now:

Code:
9992 10.10.50.0/24
  blockAdminName="Username One"
  block_name="Servers"
  blockAdminEmail="username.one@people.com"
  blockAdminPhone="312-555-1212"
  blockAdminId="1234"
  blockSecName="Username Two"
  blockSecEmail="username.two@people.com"
  blockSecPhone="312-555-2121"
  blockSecId="7891"
  blockTechName="Username Three"
  blockTechEmail="username.three@people.com"
  blockTechPhone="312-555-1313"
  blockTechId="4567"

So thank you for getting me this far. How can I modify this awk statement to get them all into one line?
# 5  
Old 04-24-2012
The awk is supposed to do that. What is your OS and version?


Code:
9993,10.10.50.0/24,blockAdminName="Username One",block_name="Servers",blockAdminEmail="username.one@people.com",blockAdminPhone="312-555-1212",blockAdminId="1234",blockSecName="Username Two",blockSecEmail="username.two@people.com",blockSecPhone="312-555-2121",blockSecId="7891",blockTechName="Username Three",blockTechEmail="username.three@people.com",blockTechPhone="312-555-1313",blockTechId="4567"
9992,10.10.40.0/24,block_name="unknown",blockAdminName="Username One",blockAdminEmail="username.one@people.com",blockAdminPhone="312-555-1212",blockAdminId="1234",blockSecName="Username Two",blockSecEmail="username.two@people.com",blockSecPhone="312-555-2121",blockSecId="7891",blockTechName="Username Three",blockTechEmail="username.three@people.com",blockTechPhone="312-555-1313",blockTechId="4567"
9991,10.10.30.0/24,block_name="unknown",blockAdminName="Username One",blockAdminEmail="username.one@people.com",blockAdminPhone="312-555-1212",blockAdminId="1234",blockSecName="Username Two",blockSecEmail="username.two@people.com"

# 6  
Old 04-27-2012
When I do it on a Red Hat system, I get that.

Red Hat Enterprise Linux ES release 4


When I try it on Solaris 10 on an M4000, I get these errors:

Code:
-bash-3.00$ awk 'p!=$1{print (p?RS:"")(p=$1),$2;} {for(i=4;i<=NF;i++)printf(i>4?" ":OFS $3 "=\"") $i; print "\"" } END{if(p)print RS}'  /tmp/uda_out_2012.3.27.164244.sql 
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1

Moderator's Comments:
Mod Comment Please click this link: How to use [code][/code] tags

Last edited by Scrutinizer; 04-27-2012 at 01:52 PM.. Reason: code tags
# 7  
Old 04-27-2012
On Solaris use /usr/xpg4/bin/awk rather than awk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Identifying a sentence and putting it on a new line

I am revisiting the problem of sentence splitting. I have a Perl Script which splits a para into sentences, but acronyms and short forms create an issue #!/usr/bin/perl use feature qw/say/; use strict; use warnings; my $s; my @arr; while(<>) { chomp $_; $s .= $_ . " "; } @arr... (2 Replies)
Discussion started by: gimley
2 Replies

3. UNIX for Dummies Questions & Answers

Putting the Current -date in the Output File

Hi guys, Just want to ask how can I make a script that will perform like this. 1. Execute the command 2. Then the output of the command will be redirected to a file 2. The file that has been created will have a date on it equivalent to the date and time it was created (or maybe after the... (5 Replies)
Discussion started by: rymnd_12345
5 Replies

4. Shell Programming and Scripting

putting color on output file script

do you have any simple script on how to change the color and font of a string in a script example echo "====================================" echo " sample color script" echo "====================================" echo " hello " echo " bye" on hello,... (3 Replies)
Discussion started by: lhareigh890
3 Replies

5. Shell Programming and Scripting

Putting echo output as input to stat

I have a string with escape differentiators as a result of searching for a file using find. Essentially find returned to my shell variable several absolute paths each ending with the file name and each path/file separated by \n. Echo recognizes the escape sequence and is able to print the paths... (3 Replies)
Discussion started by: Ebodee
3 Replies

6. Shell Programming and Scripting

How to redirect the output to multiple files without putting on console

How to redirect the output to multiple files without putting on console I tried tee but it writes to STDOUT , which I do not want. Test.sh ------------------ #!/bin/ksh echo "Hello " tee -a file1 file2 ---------------------------- $>./Test.sh $> Expected output: -------------------... (2 Replies)
Discussion started by: prashant43
2 Replies

7. Shell Programming and Scripting

Putting multiple sed commands on a single line

Hi, I want to make sed write a part of fileA (first 7 lines) to file1 and the rest of fileA to file2 in a single call and single line in sed. If I do the following: sed '1,7w file1; 8,$w file2' fileA I get only one file named file1 plus all the characters following file1. If I try to use curly... (1 Reply)
Discussion started by: varelg
1 Replies

8. Shell Programming and Scripting

Putting new line after certain number of character

Hi, I want, if a line is more than 80 characters length then put a new line with 4 space after each 80 characters to indent the data at same position. Input: 200 Geoid and gravity anomaly data of conjugate regions of Bay of Bengal and Enderby Basin: New constraints on breakup and early... (3 Replies)
Discussion started by: srsahu75
3 Replies

9. Shell Programming and Scripting

Getting command output and putting into newfile

Hello All, I am using the below code: #!/bin/sh /omp/bin/TICLI "op:alarm,all" > filename for getting command output and then putting the output into newfile but the problem is this, that not the complete output goes into newfile and the script stops. for example: if this commands gives... (18 Replies)
Discussion started by: wakhan
18 Replies

10. Shell Programming and Scripting

Putting screen output in a log file

I want to output screen messages to a logfile when executing an automated script. I have tried the script and command to do this but with no luck. Thanks, Nicole (5 Replies)
Discussion started by: nsutti
5 Replies
Login or Register to Ask a Question