Combining rows into columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining rows into columns
# 8  
Old 11-28-2013
Thanks for your responses. the post by akshey has worked like a champ for 3 columns, but it missed to print .. in between the column, but its fine.

Now the requirement has been changed, its having four columns. I m using sun solaris version 5.10. Please see my input file & output file blw:

Inputfile

Code:
servername1..Database1..Table1..column1
servername1..Database1..Table1..column2
servername1..Database1..Table1..column3
servername1..Database1..Table2..column1
servername1..Database1..Table2..column2
servername1..Database2..Table1..column1
servername1..Database2..Table1..column2
servername2..Database1..Table1..Column1
servername2..Database1..Table1..column2
servername2..Database2..Table1..column1

Output file:

Code:
servername1..Database1..Table1..column1,column2,column3
servername1..Database1..Table2..column1,column2
servername1..Database2..Table1..column1,column2
servername2..Database1..Table1..Column1,column2
servername2..Database2..Table1..column1

I have tried modifying the command which has posted shared by akshey

awk '{A[$1 FS $2 FS $3] = A[$1 FS $2 FS $3] ? A[$1 FS $2 FS $3] OFS $4 : $0}END{for(i in A){gsub(/[[:space:]]/,"..",A[i]);print A[i]}}' OFS=\, file | sort -nk1

It seem that above change in command is not correct, please correct this command which fits for 4 columns as per the requirement.


Thanks a lot for your help.
# 9  
Old 11-28-2013
For input given in #8

Code:
$ awk '{gsub(/\../," ");A[$1 FS $2 FS $3] = A[$1 FS $2 FS $3] ? A[$1 FS $2 FS $3] OFS $4 : $0}END{for(i in A){gsub(/[[:space:]]/,"..",A[i]);print A[i]}}' OFS=\,  file | sort -nk1

# 10  
Old 12-03-2013
Thanks all for your input and the code, it worked like a champ.. you help is always appreciated.. thank you so much

---------- Post updated at 07:43 PM ---------- Previous update was at 07:41 PM ----------

One more help, How to get the data into reverse (output file as an INPUT, and input file as OUTPUT). could you please help me with that.

I am not playing, I have this requriement as a part of already existing list which i need to split into each rows.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

2. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

3. Linux

[Solved] Combining columns from different files

Hey Guys & Gals, I am stuck with the following ; I have 2 text files, each containing 2 columns. My goal is to have a column from the 2nd file placed inbetween the columns in the first file. Basically the idea is, each address has a different name (but 1 name per address) but 1 address... (6 Replies)
Discussion started by: TAPE
6 Replies

4. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

Combining multiple rows in single row based on certain condition using awk or sed

Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1 "cc","b2",1 "bb","bc",2 I want the output as: "a","b","c","bc","a1","b1" "cc","cb","cc","b2" "bb","bc" I want to combine multiple lines into single line where third column is same. Is... (1 Reply)
Discussion started by: samuelray
1 Replies

6. Shell Programming and Scripting

Combining rows in a text file with a character limit

I have a file that contains several thousands rows. Here is an example. ^411912$ ^487267$ ^643776$ ^682249$ ^687737$ ^692328$ ^693767$ ^695483$ ^697289$ ^757411$ ^776688$ ^778953$ ^806123$ ^872262$ ^877877$ ^839837$ ^76666$ ^72018$ ^23330$ (3 Replies)
Discussion started by: justinb_155
3 Replies

7. Shell Programming and Scripting

combining cat output and cutting rows

I have a file that contain the following. -D HTTPD_ROOT="/usr/local/apache" -D SERVER_CONFIG_FILE="conf/httpd.conf" I want a shell script, so that after cat filename and apply the shell script I should get the output as follows. /usr/local/apache/conf/httpd.conf ie cat filename |... (7 Replies)
Discussion started by: anilcliff
7 Replies

8. UNIX for Dummies Questions & Answers

Combining two text files as columns?

I have one space delimited file with multiple columns and one tab delimited file with multiple columns (They have the same number of rows). I want to basically combine these two text files into a new text file by column. How would I go about doing that? (1 Reply)
Discussion started by: evelibertine
1 Replies

9. Shell Programming and Scripting

Combining columns from different files

I have two files I need to combine. The problem I'm having is I need to only combine data from the second file in the empty spaces of the first. For example: file1 Data Field Data Field Data Field Data Field file2 a - Insert Data b - Insert Data c - Insert Data d - Insert Data... (10 Replies)
Discussion started by: handband2
10 Replies

10. Shell Programming and Scripting

combining columns from different files

Hi all, I would be very grateful for some advice on the following. I have several text files. The files are experiment results with columns of data separated by white space. The files begin with several lines of header which are all preceeded by a comment character '#'. Each file has a... (10 Replies)
Discussion started by: iomaire
10 Replies
Login or Register to Ask a Question