Script to convert row to list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to convert row to list
# 1  
Old 06-25-2012
Script to convert row to list

Hello,
I have data which is basically a set of rows in which names having the same homophonic values are stored. I want to convert the data into lines such that the first word of the database is treated as the anchor and each subsequent word is appended to it with a column.
An example will make this clear:
The existing data is:
Quote:
aladeen=aladeena=aladin=aladina=alaudadeen=alaudadeena=alauddeen=alauddeena=alauddin
Expected output
Quote:
aladeen=aladeena
aladeen=aladin
aladeen=aladina
aladeen=alaudadeen
aladeen=alaudadeena
aladeen=alauddeen
aladeen=alauddeena
aladeen=alauddin
I have truncated the data, since in a few cases the row can have up to thirty to forty homophones.
Any help would be gratefully acknowledged. At present I am using a macro which is pretty slow and since the number of such data is very large, it is very time-consuming
Many thanks
# 2  
Old 06-25-2012
Code:
awk -F"=" '{for (i=2;i<=NF;i++) {printf $1"=";print $i}}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-25-2012
Code:
$ awk -F\= '{for(i=2;i<=NF;i++){print $1 FS $i}}' input.txt
aladeen=aladeena
aladeen=aladin
aladeen=aladina
aladeen=alaudadeen
aladeen=alaudadeena
aladeen=alauddeen
aladeen=alauddeena
aladeen=alauddin

This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 06-25-2012
Code:
 awk -F= '{x=$1;$1=z;gsub(OFS,RS x FS)}1' yourfile

These 2 Users Gave Thanks to ctsgnb For This Post:
# 5  
Old 06-25-2012
Many thanks to all for the prompt reply. If there was a delay, it is because I tested all the solutions and they all worked like magic, with the same speed.

I found they work better if gawk32 is used instead of awk or gawk.

Many thanks once again to the community and all its wonderful helpers who reply instantly and provide elegant solutions that work.
# 6  
Old 06-25-2012
Quote:
Originally Posted by ctsgnb
Code:
 awk -F= '{x=$1;$1=z;gsub(OFS,RS x FS)}1' yourfile

Interesting approach, took me a while to understand Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to convert IP range csv into a list of single IP's

Hi All, I am looking for some help to convert a csv with IP ranges in.. in the format e.g. 1.1.1.2, 1.1.1.5 2.1.1.10, 2.1.1.20 and would be looking to output as follows: 1.1.1.2 1.1.1.3 1.1.1.4 1.1.1.5 2.1.1.10 2.1.1.11 etc etc up to 2.1.1.20 I have tried a few google... (4 Replies)
Discussion started by: zippyzip
4 Replies

2. Shell Programming and Scripting

Convert Data from Column to Row

Hi FileA.txt E_TIM 16, ETE 15, EOND 26, EEC 81, E_1 un, E_2 un, E_3 un, E_4 284, E_TIM 17, ETE 15, EOND 29, EEC 82, E_1 un, E_2 un, E_3 un, E_4 249, (6 Replies)
Discussion started by: asavaliya
6 Replies

3. Shell Programming and Scripting

Script: Convert row in to column

Hi, i need to convert SG_ERP1 SG_ERP2 SG_ERP3 in to: SG_ERP1 SG_ERP2 SG_ERP3 It's possibile? (16 Replies)
Discussion started by: elilmal
16 Replies

4. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

5. Shell Programming and Scripting

Convert row to column

Hi, I have a file like this 50 1 2 1374438 50 1 2 1682957 50 5 2 1453574 50 10 2 1985890 100 1 2 737307 100 5 2 1660204 100 10 2 2148483 and I want to convert this by... (1 Reply)
Discussion started by: gvj
1 Replies

6. Shell Programming and Scripting

Convert columns to single row

Hello all I have data like 1 2 3 4 5 I wish my output would be like 1,2,3,4,5 For this i have executed 'BEGIN {FS="\n"; ORS=","} {print $0}' test and got the output as 1,2,3,4,5, I do not want to have , at the end of 5. output should be like (5 Replies)
Discussion started by: vasuarjula
5 Replies

7. Shell Programming and Scripting

Convert column values into row

hi, I have a requirement where in I read the values from a file using awk. The resulting data should be converted into row format from column format. For ex: My log file login.lst contains the following SERVER1 DB1 SERVER2 DB2 SERVER3 DB3 SERVER4 DB4 I use awk to grep only the server... (6 Replies)
Discussion started by: senthil3d
6 Replies

8. UNIX for Advanced & Expert Users

convert rows to single row

Hi I want to convert multiple rows ro single row ,I have tried with below one but I am not getting what I am expecting.Please any idea a.txt conn1=stg conn2=dev path=\xxx\a1.txt fre=a conn1=stg conn2=dev path=\xxx\a2.txt freq=a awk '/a/{ORS=" "}{print}END{print "\n"}'... (5 Replies)
Discussion started by: akil
5 Replies

9. UNIX for Dummies Questions & Answers

convert matrix to row and columns

Dear Unix Gurus, I have a sample data set that looks like this y1 y2 y3 y4 y5 x1 0.3 0.5 2.3 3.1 5.1 x2 1.2 4.1 3.5 1.7 1.2 x3 3.1 2.1 1.0 4.1 2.1 x4 5.0 4.0 6.0 7.0 1.1 I want to open it up so that I get x1 y1 0.3 x2 y1 1.2 x3 y1 3.1 x4 y1 5.0 x1 y2 0.5 x2 y2... (3 Replies)
Discussion started by: tintin72
3 Replies

10. Shell Programming and Scripting

column to row convert - script - help

Hi, I have a file named col.txt 1.000 2.000 3.000 4.000 5.000 6.000 7.000 8.000 I should get this 1.000 5.000 2.000 6.000 3.000 7.000 (10 Replies)
Discussion started by: G0Y
10 Replies
Login or Register to Ask a Question