PERL: split 2 part from singel file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL: split 2 part from singel file.
# 1  
Old 05-09-2013
Question PERL: split 2 part from singel file.

Hi,
I would like to split single fine into two array ..

Example: file.txt
--------------Installation --------------------
Code:
#GXTOOL=GxTools-20130501.tar.gz
GCSS=GExpLinux-BE-3700.0.12.37.tar.gz
TOP=TOPLinux-BE-3700.0.6.21.tar.gz
GHDER=GHDERLinux-BE-3700.0.6.20.tar.gz
ARCHIVE=ARCHIVELinux-BE-3700.0.3.21.tar.gz

--------------Fallback ------------
Code:
#GXTOOL=GxTools-20130128.6.tar.gz
GCSS=GExpLinux-BE-3600.0.7.704.tar.gz
TOP=TOPLinux-BE-3600.0.4.14.tar.gz
GHDER=GHDERLinux-BE-3600.0.4.11.tar.gz
ARCHIVE=ARCHIVELinux-BE-3600.0.0.48.tar.gz

And output should be

@a should be

--------------Installation --------------------
Code:
#GXTOOL=GxTools-20130501.tar.gz
GCSS=GExpLinux-BE-3700.0.12.37.tar.gz
TOP=TOPLinux-BE-3700.0.6.21.tar.gz
GHDER=GHDERLinux-BE-3700.0.6.20.tar.gz
ARCHIVE=ARCHIVELinux-BE-3700.0.3.21.tar.gz

@b should be

--------------Fallback ------------
Code:
#GXTOOL=GxTools-20130128.6.tar.gz
GCSS=GExpLinux-BE-3600.0.7.704.tar.gz
TOP=TOPLinux-BE-3600.0.4.14.tar.gz
GHDER=GHDERLinux-BE-3600.0.4.11.tar.gz
ARCHIVE=ARCHIVELinux-BE-3600.0.0.48.tar.gz


any advice on this ..

THanks,
Mani

Moderator's Comments:
Mod Comment Use code tags. You: Threads created: 46 - Posts: 105 - Posts Edited by a Moderator: 26 - Reminders from Moderators about formatting: 10
# 2  
Old 05-09-2013
Code:
#! /usr/bin/perl

use strict;
use warnings;

open I, "< file.txt";

my $flag = undef;
my (@a, @b);

while (<I>) {
    chomp;
    if (/Installation/) {
        $flag = 'I';
        next;
    }
    elsif (/Fallback/) {
        $flag = 'F';
        next;
    }
    
    if (/^\S+/ && $flag eq 'I') {
        push (@a, $_);
    }
    elsif (/^\S+/ && $flag eq 'F') {
        push (@b, $_);
    }
}

close I;

print "----Installation Array----\n";
for (@a) { print "$_\n" }
print "----Fallback Array----\n";
for (@b) { print "$_\n" }

# 3  
Old 05-09-2013
thanks balajesuri...

its working fine....very nice

thanks once again

THanks,
Mani
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : to split the tags from xml file

I do have an xml sheet as below where I need the perl script to filter only the hyperlink tags. <cols><col min="1" max="1" width="30.5703125" customWidth="1"/><col min="2" max="2" width="7.140625" bestFit="1" customWidth="1"/> <col min="3" max="3" width="32.28515625" bestFit="1"... (3 Replies)
Discussion started by: scriptscript
3 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Perl Question - split function with csv file

Hi all, I have a csv file that appears as follows: ,2013/03/26,2012/12/26,4,1,"2017/09/26,5.75%","2017/09/26,1,2018/09/26,1,2019/09/26,1,2020/09/26,1,2021/09/26,1",,,2012/12/26,now when i use the split function like this: my @f = split/,/; the split function will split the data that is... (2 Replies)
Discussion started by: WongSifu
2 Replies

3. Shell Programming and Scripting

perl script to split the text file after every 4th field

I had a text file(comma seperated values) which contains as below 196237,ram,25-May-06,ram.kiran@xyz.com,204183,Pavan,4-Jun-07,Pavan.Desai@xyz.com,237107,ram Chandra,15-Mar-10,ram.krishna@xyz.com ... (3 Replies)
Discussion started by: giridhar276
3 Replies

4. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

5. Shell Programming and Scripting

How do I split file into pieces with PERL?

How do I split file into pieces with PERL? IE file.txt head 1 2 3 4 end head 5 6 7 8 9 end n so on (7 Replies)
Discussion started by: 3junior
7 Replies

6. Shell Programming and Scripting

Perl Split for text in file

Hi all I have written Perl script to swap the strings in the second a third column from a text file. My input file format is : the|empty|the|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|empty|bird|s|nn The expected output file format is... (11 Replies)
Discussion started by: my_Perl
11 Replies

7. Shell Programming and Scripting

Perl or Awk script to copy a part of text file.

Hi Gurus, I'm a total newbie to Perl and Awk scripting. Let me explain the scenario, there is a DB2 table with 5 columns and one of the column is a CLOB datatype containing XML. We need all the 4 columns but only a portion of string from the XML column. We decided to export DB2 table to a .del... (26 Replies)
Discussion started by: asandy1234
26 Replies

8. Shell Programming and Scripting

how to get split output of a file, using perl script

Hi, I have file: data.log.1 ### s1 main.build.3495 main.build.199 main.build.3408 ###s2 main.build.3495 main.build.3408 main.build.199 I want to read this file and store in two arrays in Perl. I have following command, which is working fine on command prompt. perl -n -e... (1 Reply)
Discussion started by: ashvini
1 Replies

9. Shell Programming and Scripting

Help fix my garbage - File Split Program in Perl

Hi, I have the following, it doesn't work and I know it's crap code. The objective is to split a file with a givin number of codes such as: 01,02,03,...,99 Then return all records with each seperate identifier in a new file. The files being split have lrecl=500, recfm=F, and I... (4 Replies)
Discussion started by: mkastin
4 Replies

10. Shell Programming and Scripting

perl help to split big verilog file into smaller ones for each module

Hi I have a big verilog file with multiple modules. Each module begin with the code word 'module <module-name>(ports,...)' and end with the 'endmodule' keyword. Could you please suggest the best way to split each of these modules into multiple files? Thank you for the help. Example of... (7 Replies)
Discussion started by: return_user
7 Replies
Login or Register to Ask a Question