Converting rows into multiple-rows


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Converting rows into multiple-rows
# 1  
Old 11-13-2009
Converting rows into multiple-rows

Hi every one;

I have a file with 22 rows and 13 columns which includes floating numbers.
I want to parse the file so that every five columns in the row would be a new record (row). For example, the first line in the old file should be converted into three lines with first two lines contain 5 fields and 3rd line with three fields. The process continues for all 22 lines.
The new file will have the following dimension: 66 rows and 5 columns.

Please help and let me know if you want more clarification.
# 2  
Old 11-13-2009
Yes,
could you please post sample data and the expected output Smilie
# 3  
Old 11-13-2009
input sample with two lines and 23 columns:
Code:
1  2  3   4  5  6   7  8  9 10 11 12 13
14 15 16 17 18 19 20 21 22 23 24 25 26

Expected output:
Code:
1  2  3  4   5
6  7  8  9  10
11 12 13
14 15 16 17 18
19 20 21 22 23
24 25 26

The output would have five columns (except records which are multiples of three since the each record in input file has 13 columns and 13%5=3).

Thanks

Last edited by PHL; 11-13-2009 at 04:53 PM.. Reason: Formatting
# 4  
Old 11-13-2009
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.

Code:
awk '{ 
  for (i=1; i<=NF; i++)
    printf "%s", $i (!(i % 5) || i == NF ? RS : FS) 
    }' infile

# 5  
Old 11-13-2009
Code:
awk 'function format(i){ retval=(i%5==0)?" %d\n":"%d "; return retval}
       {for(i=1;i<=13; i++) {printf(format(i),$i)} printf("\n") } ' oldfile > newfile

I like radulov's version better.
# 6  
Old 11-14-2009
given SED a try....

Code:
sed 's@\([^ ]\+\s\+\)\{5\}@&\n@g'  t1


Code:
$ cat t1
1  2  3   4  5  6   7  8  9 10 11 12 13
14 15 16 17 18 19 20 21 22 23 24 25 26


$ sed 's@\([^ ]\+\s\+\)\{5\}@&\n@g'  t1
1  2  3   4  5
6   7  8  9 10
11 12 13
14 15 16 17 18
19 20 21 22 23
24 25 26

# 7  
Old 11-14-2009
Small modification with the geeks sed solution should make ik work with tabs too:

Code:
sed 's/\([^ \t]\+[ \t]\+\)\{5\}/&\n/g' infile

-or-
Code:
sed -r 's/([^ \t]+[ \t]+){5}/&\n/g' infile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting rows into columns

hi all I need a help ..I have a script that takes has command and its output is like below.. a b a v v a c I am assigning the above outputs to a variable .. <variable name> = <command output> problem here is when I echo the variable ..it gives me output like " a b... (3 Replies)
Discussion started by: shankarb
3 Replies

2. Shell Programming and Scripting

Converting Single Column into Multiple rows

Hi .. anyone can you help me ? i need to convert text below into multiple columns interface; GigabitEthernet0/0/0/0 description; TRUNK_PE-D2-JT2-VPN_Gi0/0/0/0_TO_ME4-A-JKT-JT_4/1/1_1G mtu 9212 negotiation auto interface; GigabitEthernet0/0/0/0.11 description; tes encapsulation;... (1 Reply)
Discussion started by: mad3linux
1 Replies

3. Shell Programming and Scripting

Converting a single row to multiple rows

Hi, I want to convert a single row values to multiple rows, but the no. of rows are not fixed. For example, I have a row as below abc-def-lmn-mno-xyz out put should be get abc get def get lmn get xyz (4 Replies)
Discussion started by: Suneel Mekala
4 Replies

4. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

5. Shell Programming and Scripting

Converting rows to column

i have output of script as below name,roll_no,01-05-12,02-05-12,03-05-12 sam,12,24,24,24 jon,145,24,24,22 van,29,24,22,24 i want to convert these into columns as output is not fixed please tell me how to convert 1st row in to 1st columns likewise,as many rows are there are to be converted... (4 Replies)
Discussion started by: sagar_1986
4 Replies

6. Shell Programming and Scripting

Converting Multiple rows to Single Row using unix commands

Can somebody help me in solving this.. Input data is like 0 A 1 B 2 C 3 D 0 A1 1 B1 2 C1 3 D1 0 A2 1 B2 2 C2 3 D2 Output should be like A B C D A1 B1 C1 D1 A2 B2 C2 D2 (7 Replies)
Discussion started by: Mahantesh Patil
7 Replies

7. Shell Programming and Scripting

Split single rows to multiple rows ..

Hi pls help me out to short out this problem rm PAB113_011.out rm: PAB113_011.out: override protection 644 (yes/no)? n If i give y it remove the file. But i added the rm command as a part of ksh file and i tried to remove the file. Its not removing and the the file prompting as... (7 Replies)
Discussion started by: sri_aue
7 Replies

8. Shell Programming and Scripting

converting rows into columns

Hi, I am trying to fetch some values from db and spooling the output to a file. when i query the db for the values, i get the values in following format. PC_1 wf_test1 Test PC_2 wf_test2 Test PC_3 wf_test3 Test But my spool file was created in following format. PC_1 wf_test1 Test... (20 Replies)
Discussion started by: svajhala
20 Replies

9. UNIX for Dummies Questions & Answers

Converting columns into rows

Is there anyway to convert columns into raws using awk? (or any other command line):eek::eek::eek::eek::eek::eek::eek::eek::eek: (1 Reply)
Discussion started by: cosmologist
1 Replies

10. Shell Programming and Scripting

Converting Single Column into Multiple rows

i have single column which is starting with same string(many number of rows) i have to convert each into a single row.how can i do that? laknar std mes 23 55 laknar isd phone no address amount 99 I have to convert above like below. laknar|std|mes|23|55 laknar|isd|phone... (3 Replies)
Discussion started by: laknar
3 Replies
Login or Register to Ask a Question