Perl split and add new line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl split and add new line
# 1  
Old 11-21-2013
Perl split and add new line

Hi All,

I find the below code printing the output of a particular field in same line, which i want it to be printed on a new line.
---CODE START---
Code:
    foreach $defectRec (@outLs) {
      my($def_id,$status,$files_mod) = split(/,/, $defectRec);
      print "DEFECT ID: $def_id, Status: $status, Files Mod: $files_mod\r\n" if( defined ($debug) );

----CODE END----

OUTPUT when redirected to a CSV file prints as:
-----OUTPUT BEGIN------
Code:
DEFECT_ID Status Files_Modified
20222        Testing File1;File2;File3
20333        Testing File4;File6

-----OUTPUT END------

Am good with output but i want the column containing Files_Modified field to split as follows:

-----EXPECTED OUTPUT BEGIN------
Code:
DEFECT_ID Status Files_Modified
20222        Testing File1;
                           File2;
                           File3
20333        Testing File4;
                           File6

-----EXPECTED OUTPUT END------

As the output is redirected to a CSV file, please let me know how i can split values of Files_modified attribute to print in new line.

Thanks,
nmattam

Last edited by Franklin52; 11-21-2013 at 09:36 AM.. Reason: Please use code tags
# 2  
Old 11-21-2013
Hi,
It's not the good part of perl code. the print line is execute only on debug mode and I expect output is:
Code:
DEFECT ID: 20222, Status: Testing, Files Mod: File1;File2;File3

Regards.
# 3  
Old 11-22-2013
perl split and add new line

HI disedorgue,

Thanks for your reply. Is there a way i can redirect my output to a csv file as:

DEFECT ID Status: Files Mod
20222 Testing File1
File2;
File3

Thanks,
nmattam
# 4  
Old 11-24-2013
Hi,
If I respect your code (change in red):
Code:
$ cat cc3cc.pl
@outLs=<>; # <= add line just for example (no need in your code)
    foreach $defectRec (@outLs) {
      my($def_id,$status,$files_mod) = split(/,/, $defectRec);
      print "DEFECT ID: $def_id, Status: $status, Files Mod: $files_mod\r\n" if( defined ($debug) );
      $_= $files_mod;
      $_=~ s/;/;\r\n  /g;
      print "$def_id $status $_";
} # <== idem first line

input example:
Code:
$ cat cc2cc.vv
DEFECT_ID,Status,Files_Modified
20222,Testing,File1;File2;File3
20333,Testing,File4;File6

and then:
Code:
$ perl cc3cc.pl cc2cc.vv
DEFECT_ID Status Files_Modified
20222 Testing File1;
  File2;
  File3
20333 Testing File4;
  File6

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add specific string to last field of each line in perl based on value

I am trying to add a condition to the below perl that will capture the GTtag and place a specific string in the last field of each line. The problem is that the GT value used is not right after the tag rather it is a few fields away. The values should always be 0/1 or 1/2 and are in bold in the... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

How to add line breaks to perl command with large text in single quotes?

Below code extracts multiple field values from XML into array and prints all in one line. perl -nle '@r=/(?: jndiName| authDataAlias| value| minConnections| maxConnections| connectionTimeout| name)="(+)/g and print join ",",$ENV{tIPnSCOPE},$ENV{pr ovider},$ENV{impClassName},@r' server.xml ... (4 Replies)
Discussion started by: kchinnam
4 Replies

3. Shell Programming and Scripting

How to add a line in every file with perl commandline?

Hi, i want to add a line at the beginning of every file in a directory. perl -i.bkp -p -e 'print "#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset\n" if $. == 1' *.reset But this command is updating only the first file in the directory. I think this is because $. is not resetting... (3 Replies)
Discussion started by: twistedpair
3 Replies

4. Programming

Perl find text and add line

Hi All I need to add a line to a file but after a certain block of text is found The block of text looks like this <RDF:Description RDF:about="urn:mimetype:video/quicktime" NC:value="video/quicktime" and i need to add this in the next line down ( note there is... (4 Replies)
Discussion started by: ab52
4 Replies

5. Shell Programming and Scripting

How to add a line in a file using perl script?

Hi all, I want to search for a line in a file using perl script and add a line above that line.Can any one suggest me command or code to that using script. Thanks BHarath (5 Replies)
Discussion started by: bharathece
5 Replies

6. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

7. Shell Programming and Scripting

Split a single record to multiple records & add folder name to each line

Hi Gurus, I need to cut single record in the file(asdf) to multile records based on the number of bytes..(44 characters). So every record will have 44 characters. All the records should be in the same file..to each of these lines I need to add the folder(<date>) name. I have a dir. in which... (20 Replies)
Discussion started by: ram2581
20 Replies

8. Shell Programming and Scripting

perl script to add a line into a file

hi all need a perl script to add a line into a file thanks with anticipation (2 Replies)
Discussion started by: karthikn7974
2 Replies

9. Shell Programming and Scripting

Perl : split flat line's to $

Hi, below is the software list of lines I have, MozillaSrc 1.7.8.00.00 Mozilla 1.78 Source Distribution NFS B.11.11 ONC/NFS; Network-File System,Information Services,Utilities NParProvider B.11.11.01.04.01.01 nPartition Provider NPartition ... (7 Replies)
Discussion started by: acomd2
7 Replies

10. Shell Programming and Scripting

add to first line of file using perl

Hey guys, I have a text file full of data and all I want to do with the thing is add the file extension name to the first line of the file. Here is what I have so far. I can't remember how to append to the first line of the file without deleting the exsisting data. #!/usr/local/bin/perl ... (5 Replies)
Discussion started by: kingdbag
5 Replies
Login or Register to Ask a Question