Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Make Multile line is one line using window Perl

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 12-28-2012
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
# 2  
Old 12-28-2012
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.
# 3  
Old 12-28-2012
Is it possible to do without CSV lib.
Everything is Possible in Perl
# 4  
Old 12-28-2012
Possible, of course, but it smacks of intellectual laziness, lack of desire to learn or reuse. Perhaps API specs confuse him. Next!
# 5  
Old 12-28-2012
Can you please find the error, in above code. I think with some tweaking in the above code , it will start working.
# 6  
Old 12-29-2012
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
# 7  
Old 12-31-2012
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);

Login to Reply

« Previous Thread | Next 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 command line option '-n','-p' and multiple files: can it know a file name of a printed line? alex_5161 Shell Programming and Scripting 6 06-01-2017 03:18 PM
Perl to extract information from a file line by line cmccabe Shell Programming and Scripting 1 09-10-2016 09:30 AM
Perl : Assigning multile hash values to a single array popeye Shell Programming and Scripting 2 01-26-2014 05:36 PM
Concatenate small line with next line perl script Ophiuchus Shell Programming and Scripting 2 11-10-2013 02:35 AM
perl: comparision of field line by line in two files Thelost Shell Programming and Scripting 8 05-11-2012 10:03 AM
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 merge multi-line into single line happyday Shell Programming and Scripting 3 05-11-2009 04:42 AM
Perl script to search a line and copy it to another line ammu Shell Programming and Scripting 3 12-29-2008 04:12 PM
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 12:21 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password