Make Multile line is one line using window Perl | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Make Multile line is one line using window Perl

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-28-2012
adisky123 adisky123 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 December 2013, 5:57 AM EST
Posts: 58
Thanks: 5
Thanked 0 Times in 0 Posts
Make Multile line is one line using window Perl

Hi All

I have a .csv file which is showing data as


Code:
ESP Client,ESP Engagement,Misc_Projects_120101,DEFAULT,HA,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected-Done,"she,joy.",200111,Unknown,Full Time,,Delivery_DONE AMO,Approved,2012-12-03,2012-12-06,2012-12-06,"Occupied Hours 
(0)",0,"Approved Hours 
(112)",8,"Pending Hours 
(0)",0,"Pending and Approved Hours 
(112)",8,

ESP Client,ESP Engagement,Misc Projects_120101,DEFAULT,HR,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected - Pending,"she, aj v.",200111,Unknown,Full Time,,Delivery_Pending AMO,Approved,2012-12-04,2012-12-14,2012-12-14,"Occupied Hours 
(0)",0,"Approved Hours 
(112)",8,"Pending Hours 
(0)",0,"Pending and Approved Hours 
(112)",8,

I want to make in one line like using windows Perl

Code:
ESP Client,ESP Engagement,Misc_Projects_120101,DEFAULT,HA,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected-Done,"she,joy.",200111,Unknown,Full Time,,Delivery_DONE AMO,Approved,2012-12-03,2012-12-06,2012-12-06,"Occupied Hours (0)",0,"Approved Hours(112)",8,"Pending Hours(0)",0,"Pending and Approved Hours(112)",8,
ESP Client,ESP Engagement,Misc Projects_120101,DEFAULT,HR,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected - Pending,"she, aj v.",200111,Unknown,Full Time,,Delivery_Pending AMO,Approved,2012-12-04,2012-12-14,2012-12-14,"Occupied Hours(0)",0,"Approved Hours(112)",8,"Pending Hours(0)",0,"Pending and Approved Hours(112)",8,

What I have tried is

Code:
open('NEW',"<$new_file") || die "Error open file $new_file\n";

  while (my $s2=<NEW>) {
      if($s2 =~ /ESP General Client/){
          
          if ($flag==1){
              print NEW1 $new;
              $flag=0;
          }
          my $new=$s2;
      }
      else{
          $new .= ''.$s2;
          $flag=1;
      }
   }
    close(NEW);


But its not working

---------- Post updated at 09:01 AM ---------- Previous update was at 08:59 AM ----------

Sorry the actual code I am using is
open('NEW',"<$new_file") || die "Error open file $new_file\n"; while (my $s2=<NEW>) { if($s2 =~ /ESP Client/){ if ($flag==1){ print NEW1 $new; $flag=0; } my $new=$s2; } else{ $new .= ''.$s2; $flag=1; } } close(NEW);

---------- Post updated at 09:02 AM ---------- Previous update was at 09:01 AM ----------

Sorry the actual code i am using is

Code:
open('NEW',"<$new_file") || die "Error open file $new_file\n";

  while (my $s2=<NEW>) {
      if($s2 =~ /ESP Client/){
          
          if ($flag==1){
              print NEW1 $new;
              $flag=0;
          }
          my $new=$s2;
      }
      else{
          $new .= ''.$s2;
          $flag=1;
      }
   }
    close(NEW);

---------- Post updated at 12:24 PM ---------- Previous update was at 09:02 AM ----------

Its Make Multile line to one line using window Perl
Sponsored Links
    #2  
Old 12-28-2012
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 8 July 2014, 12:19 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,378
Thanks: 8
Thanked 535 Times in 514 Posts
The linefeeds are inside quotes, so the PERL CSV lib can pick them up, and all you need to do is replace line feed with space inside the fields.

Doing CSV in PERL without using the CSV libs seems like bad form, reinventing the wheel. If there were quoted commas in your fields, this would fall apart, so it is not robust.
Sponsored Links
    #3  
Old 12-28-2012
adisky123 adisky123 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 December 2013, 5:57 AM EST
Posts: 58
Thanks: 5
Thanked 0 Times in 0 Posts
Is it possible to do without CSV lib.
Everything is Possible in Perl
    #4  
Old 12-28-2012
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 8 July 2014, 12:19 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,378
Thanks: 8
Thanked 535 Times in 514 Posts
Possible, of course, but it smacks of intellectual laziness, lack of desire to learn or reuse. Perhaps API specs confuse him. Next!
Sponsored Links
    #5  
Old 12-28-2012
adisky123 adisky123 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 December 2013, 5:57 AM EST
Posts: 58
Thanks: 5
Thanked 0 Times in 0 Posts
Can you please find the error, in above code. I think with some tweaking in the above code , it will start working.
Sponsored Links
    #6  
Old 12-29-2012
bvsarathi's Avatar
bvsarathi bvsarathi is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 12 February 2013, 4:29 AM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
perl -lne "$line=$line.' '.$_;if(/^$/ or eof){print $line;undef $line;}" your_file

Regards,
Vijay

Last edited by Scott; 12-30-2012 at 12:42 AM.. Reason: Removed link
Sponsored Links
    #7  
Old 12-31-2012
adisky123 adisky123 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 December 2013, 5:57 AM EST
Posts: 58
Thanks: 5
Thanked 0 Times in 0 Posts
Finally this thread ends up with this working code

Code:
my $new='';
$flat_file='flat_file.csv';
$new_file='new_file.csv';

open('NEW',"<$new_file") || die "Error open file $new_file\n";
open('NEW1',">$flat_file") || die "Error open file $flat_file\n";

  while (my $s2=<NEW>) {
       chomp($s2);
      if($s2 =~ /ESP Client/){
        print NEW1 "$new\n"; $new='';
                $new=$s2;
      }
      else{
          $new = $new.$s2;
      }
   }
    close(NEW);
    close(NEW1);

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
PERL or SHELL Scrript to search in Directories by taking line by line from a text file pasam Shell Programming and Scripting 9 12-29-2011 02:30 AM
How to use Perl to join multi-line into single line happyday Shell Programming and Scripting 5 11-10-2009 01:44 AM
How to use Perl to merge multi-line into single line happyday Shell Programming and Scripting 3 05-11-2009 04:42 AM
make multiple line containing a pattern into single line VTAWKVT Shell Programming and Scripting 13 12-04-2008 05:40 PM
Multile Pattern Search in a same line and delete sasree76 Shell Programming and Scripting 2 04-16-2008 02:12 PM



All times are GMT -4. The time now is 09:04 PM.