Converting a list to a row delimited


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting a list to a row delimited
# 1  
Old 09-04-2012
Converting a list to a row delimited

Hello,
I have a large database with the following structure:
set of clustered names followed by a hard return and followed by a second set of clustered names and so on. Sometimes the clusters can be as many as 150.
Since the data is in an Indian language, a theoretical example will make this clear.
Quote:
john
jon
jhon

mary
marry
meri
mari
What I need is to convert the series of lists to a single row so that the desired output is as under:
Quote:
john=jon=jhon

mary=merry=meri=mari
I have been using a simple macro to do the job, but given the size of the file which runs to over 300 thousand names, the macro is too slow and takes time.
A perl or awk script would be most helpful.
Many thanks in advance
# 2  
Old 09-04-2012
Naive way:
Code:
 awk '{a=""; for(i=1;i<=NF;i++) a=a?(a "=" $i):$i ; print a}'  RS= names.txt

or more elegant:
Code:
awk '$1=$1' OFS="=" RS= names.txt


Last edited by mirni; 09-04-2012 at 02:11 AM..
This User Gave Thanks to mirni For This Post:
# 3  
Old 09-04-2012
Hi


Code:
$ paste -sd= file | sed 's/==/\n/g'
john=jon=jhon
mary=marry=meri=mari

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 4  
Old 09-04-2012
you can try this...
Code:
$FILE_NAME="abc.txt";
$/="\n\n";

open(FH_R,"<",$FILE_NAME)|| die "Unable to open $FILE_NAME for reading: $!\n";
while (<FH_R>)
{
        chomp;
        s/\n+/=/g;
        print "$_\n";
}
close FH_R;


Last edited by Franklin52; 09-04-2012 at 04:16 AM.. Reason: Please use code tags for data and code samples, thanks
This User Gave Thanks to replytoshishir For This Post:
# 5  
Old 09-04-2012
Tools

HI ....

Try this untested perl script.....
Code:
open (FILE,"file1");
while(<FILE>)
{
   $line = $_;
   if ( $line =~ /^\s*$/ )
   {
       print "$value\n";
       $value = "";  @value = ();
   }
   else
   {
     push (@value,$line);
     $value = join "=",@value;
   }
}
close(FILE);

Thanks
Anusurya.ASmilie

Last edited by Franklin52; 09-04-2012 at 03:49 AM.. Reason: Please use code tags for data and code samples
This User Gave Thanks to anusurya For This Post:
# 6  
Old 09-04-2012
Code:
$tr '\n' "=" < file | sed 's/==/\n/g'
john=jon=jhon
mary=marry=meri=mari

This User Gave Thanks to pamu For This Post:
# 7  
Old 09-04-2012
Code:
awk 'NF==0{f=0;printf "\n";next}{if(f==1){printf "="$1}else{f=1;printf $1}}' filename

This User Gave Thanks to raj_saini20 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search and delete a row which is delimited by |^

I have a sample text file like this. CampaignId|^CampaignCd|^InsertionOrderCd|^OwningAdvertiserCd|^CampaignName 998201|^T15-06|^T15|^|^GTA 160x160 998277|^T15-07|^T15|^TEST|^GTA 160x160 998297|^T15-07|^T15|^TEST2|^GTA 160x160 I want to delete the line only when the 4th field is empty. ... (2 Replies)
Discussion started by: Tuxidow
2 Replies

2. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

Converting varied space delimited file to Pipedemilited

Hi all, I have source file, data looks like 12345 abc def 01 / 001200 C 2000 12345 abc def 01 / 001200 C 2500 12345 abcd def 01 / 001200 C 3500 18945 xyz pqr 01 / 009900 D 4000 5000 2800 9900 Expected ouput... (3 Replies)
Discussion started by: srk409
3 Replies

4. Shell Programming and Scripting

Converting a list to a row to create clusters based on numerical identity

Hello. I have a long list of data which has the following structure The number shows the unique identity of the word. And all homophones are clustered with the same number ID. An example will make this clear The awk script I have allows conversion of a list to row but on condition that each... (4 Replies)
Discussion started by: gimley
4 Replies

5. Shell Programming and Scripting

converting column to row

Hi everyone.. I have a list of values in a file... 1.2345e-1 2.282828e+ 3.2341e-1 1.1223445e-1 I am interested in converting this column to a row.. 1.2345e-1 2.282828e+ 3.2341e-1 1.1223445e-1 can anyone pls help?? I am a liunx newbie.. Thanks.. (7 Replies)
Discussion started by: kjha
7 Replies

6. Shell Programming and Scripting

Help with converting Pipe delimited file to Tab Delimited

I have a file which was pipe delimited, I need to make it tab delimited. I tried with sed but no use cat file | sed 's/|//t/g' The above command substituted "/t" not tab in the place of pipe. Sample file: abc|123|2012-01-30|2012-04-28|xyz have to convert to: abc 123... (6 Replies)
Discussion started by: karumudi7
6 Replies

7. UNIX for Advanced & Expert Users

Converting rows to a single row

Hi all I have a file as below : Development System User Production i want to convert the file to below format: "Development","System","User","Production" Is it possible with UNIX ? if so can you please give me some direction on it ? Thanks, Satya Use code tags please, ty. (10 Replies)
Discussion started by: satyaranjon
10 Replies

8. Shell Programming and Scripting

Converting comma separated to pipe delimited file

Hi, I came across a very good script to convert a comma seperated to pipe delimited file in this forum. the script serves most of the requirement but looks like it does not handle embedded double quotes and commas i.e if the input is like 1234, "value","first,second", "LDC5"monitor",... (15 Replies)
Discussion started by: anijan
15 Replies

9. UNIX for Dummies Questions & Answers

Converting Space delimited file to Tab delimited file

Hi all, I have a file with single white space delimited values, I want to convert them to a tab delimited file. I tried sed, tr ... but nothing is working. Thanks, Rajeevan D (16 Replies)
Discussion started by: jeevs81
16 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question