Solaris rows to columns question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Solaris rows to columns question
# 1  
Old 06-13-2012
[SOLVED] Solaris rows to columns question

Hey all, I know theres a few posts already here about converting rows to columns but I can't find exactly what I'm looking for..


In a file I want to keep the first two fields of each line and then put everything else after those two fields into a separate line like below. I can't get my head around how to do this as there won't always be the same amount of columns for each line.. How do you count that?

File before
Quote:
root ALL(ALL) ALL
KVHPOJNOTES KVHPOJNOTESSERVER C_POJ_NOTES_C1D63EN
C_POJ_NOTES_C99HXEN C_POJ_NOTES_R656JP C_POJ_NOTES_SMD C_POJ_NOTES_UP3 C_POJ_NOTES_UP1 C_POJ_NOTES_SMD3
INES KVHPOJNOTESSERVER(ALL) ALL
KVHNOTESIDANAPC KVHPOJANAPC C_POJ_NOTES_START_ANAPC C_POJ_NOTES_STOP_ANAPC
KVHNOTESIDANAPD KVHPOJANAPD C_POJ_NOTES_START_ANAPD C_POJ_NOTES_STOP_ANAPD

File after
Quote:
root ALL(ALL) ALL
KVHPOJNOTES KVHPOJNOTESSERVER C_POJ_NOTES_C1D63EN
KVHPOJNOTES KVHPOJNOTESSERVER C_POJ_NOTES_C99HXEN
KVHPOJNOTES KVHPOJNOTESSERVER C_POJ_NOTES_R656JP
KVHPOJNOTES KVHPOJNOTESSERVER C_POJ_NOTES_SMD
KVHPOJNOTES KVHPOJNOTESSERVER C_POJ_NOTES_UP3
KVHPOJNOTES KVHPOJNOTESSERVER C_POJ_NOTES_UP1
KVHPOJNOTES KVHPOJNOTESSERVER C_POJ_NOTES_SMD3
INES KVHPOJNOTESSERVER(ALL) ALL
KVHNOTESIDANAPC KVHPOJANAPC C_POJ_NOTES_START_ANAPC
KVHNOTESIDANAPC KVHPOJANAPC C_POJ_NOTES_STOP_ANAPC
KVHNOTESIDANAPD KVHPOJANAPD C_POJ_NOTES_START_ANAPD
KVHNOTESIDANAPD KVHPOJANAPD C_POJ_NOTES_STOP_ANAPD
Any help at all would be much appreciated..

Thanks,
Jaz

Last edited by Jazmania; 06-13-2012 at 12:52 PM..
# 2  
Old 06-13-2012
could you post a smaller, more easily understandable example
# 3  
Old 06-13-2012
Quote:
Originally Posted by jamie_123
could you post a smaller, more easily understandable example
Ok, below is as simple as I can put it.. The only thing is there will be multiple lines and not every line in the file will have 5 fields..

Quote:
Before
Field1 Field2 Field3 Field4 Field5


After
Field1 Field2 Field3
Field1 Field2 Field4
Field1 Field2 Field5
# 4  
Old 06-13-2012
How about this-

Code:
$ cat try.txt 
Field1 Field2 Field3 Field4 Field5
Field1 Field2 Field3 Field4 
Field1 Field2 Field3 
Field1 Field2 Field3 Field4 Field5


$ awk '{ if (NF == 3) print $1 " " $2 " " $3; if (NF == 4) print $1 " " $2 " " $3 "\n" $1 " " $2 " " $4; if (NF == 5) print $1 " " $2 " " $3 "\n" $1 " " $2 " " $4 "\n" $1 " " $2 " " $5}' try.txt 
Field1 Field2 Field3
Field1 Field2 Field4
Field1 Field2 Field5
Field1 Field2 Field3
Field1 Field2 Field4
Field1 Field2 Field3
Field1 Field2 Field3
Field1 Field2 Field4
Field1 Field2 Field5

# 5  
Old 06-13-2012
Quote:
Originally Posted by jamie_123
How about this-

Code:
$ cat try.txt 
Field1 Field2 Field3 Field4 Field5
Field1 Field2 Field3 Field4 
Field1 Field2 Field3 
Field1 Field2 Field3 Field4 Field5


$ awk '{ if (NF == 3) print $1 " " $2 " " $3; if (NF == 4) 
print $1 " " $2 " " $3 "\n" $1 " " $2 " " $4; if (NF == 5) 
print $1 " " $2 " " $3 "\n" $1 " " $2 " " $4 "\n" $1 " " $2 " " $5}' try.txt 
Field1 Field2 Field3
Field1 Field2 Field4
Field1 Field2 Field5
Field1 Field2 Field3
Field1 Field2 Field4
Field1 Field2 Field3
Field1 Field2 Field3
Field1 Field2 Field4
Field1 Field2 Field5


Hmmm, it kind of works.. It didn't process the second line in the file which has 6 fields.. And the thing is it won't be known what the max number of fields could be.. I need to be able to do some sort of loop or count first I think.. I just don't know how that would be done..
# 6  
Old 06-13-2012
This should do fine anywhere-
Code:
$ cat try.txt 
Field1 Field2 Field3 Field4 Field5
Field1 Field2 Field3 Field4 
Field1 Field2 Field3 
Field1 Field2 Field3 Field4 Field5


$ awk '{if (NF>2) for (i=3; i<=NF; i++) {print $1, $2, $i}}' try.txt 
Field1 Field2 Field3
Field1 Field2 Field4
Field1 Field2 Field5
Field1 Field2 Field3
Field1 Field2 Field4
Field1 Field2 Field3
Field1 Field2 Field3
Field1 Field2 Field4
Field1 Field2 Field5

HTH
This User Gave Thanks to jamie_123 For This Post:
# 7  
Old 06-13-2012
Perfect my good man.. Many thanks for that...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Columns to rows

HI UNIX Gurus, Not sure if this was already asked and an UNIX Guru has replied but I could not find what i wanted. I have linux environment and need help on this. I have several files like this. a,1 b,1 utc,10/12/2019 local,10/12/2018 name,xxxy deg,feh 10,12 20,8 30,50 32,64 46,65... (5 Replies)
Discussion started by: Roopensingh
5 Replies

2. UNIX for Beginners Questions & Answers

Rows to columns

trying to sort an array, only the 4th column is uniq, have this: dog cat bird house1 dog cat bird house2 dog cat bird house3 rose daisy tulip house1 rose daisy tulip house3 would like this: dog cat bird house1 house2 house3 rose daisy tulip house1 missing house3 any help... (6 Replies)
Discussion started by: jimmyf
6 Replies

3. 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

4. Shell Programming and Scripting

Columns and rows

echo "Month" echo "Feb_2014" echo "March_2014" echo "Apr_2014" echo "No of files" grep ,y, Feb_2014.csv |wc -l grep ,y, Mar_2014.csv |wc -l grep ,y, Apr_2014.csv |wc -l echo "Total no of success" awk -F"," '{x+=$4}END{print x}' Feb_2014.csv awk -F"," '{x+=$4}END{print x}'... (3 Replies)
Discussion started by: shelllearner
3 Replies

5. Shell Programming and Scripting

Columns to rows

Hi, I have a big file, with thousands of rows, and I want to put every 7 rows in a line. Input file: str1, val2, val3 str2, val4, val5 str3, val22, val33 str4, val44, val55 str5, val6, val7 str6, val77, val88 str7, val99, val00 str1, som2, som3 str2, som4, som5 str3, som22, som33 ... (11 Replies)
Discussion started by: apenkov
11 Replies

6. 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

7. 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

8. Shell Programming and Scripting

Rows to Columns

Hi Guru's, I have a requirement where i need to convert rows to column based on a key column. Input: Account_id|Trip_Org|Trip_Dest|City|Hotel_Nm 123|DFW|CHI|Dallas|Hyatt 123|LAS|LPA|Vegas|Hyatt Palace Output:... (6 Replies)
Discussion started by: rakesh5300
6 Replies

9. Windows & DOS: Issues & Discussions

Columns to Rows

I want to create a script with gawk. I have the following file with 2 columns: A 1 A 2 A 3 B 1 B 2 B 3 C 1 C 2 D 1 D 2 D 3 D 4 and i want to convert to: (1 Reply)
Discussion started by: sameeribraimo
1 Replies

10. Shell Programming and Scripting

# of rows and columns

Hi, Does anyone know the command to know the # of rows and columns for a file? thanks (3 Replies)
Discussion started by: kylle345
3 Replies
Login or Register to Ask a Question