Sponsored Content
Top Forums Shell Programming and Scripting How to concatenate texts in perl only? Post 302557348 by vanitham on Wednesday 21st of September 2011 01:50:26 AM
Old 09-21-2011
Quote:
Originally Posted by birei
Hi,

Try next 'Perl' program. Here a test in my computer:
Code:
$ for file in scenario[1-4]; do cat -n "$file"; echo "----------"; done
     1  12345`English@|1@|perl is a scripting language@|English
     2  12345`English@|2@|Regular expression support is vast@|English
     3  12345`English@|3@|Has various items@|English
     4  12345`English@|4@|xml support is good@|English
----------
     1  12345`English@|1@|DC@|34@|4456@|Text needs to be concatenated@|English
     2  12345`English@|2@|FC@|36@|4455@|Text needs to be concatenated-1@|English
     3  12345`English@|3@|BC@|39@|4453@|Text needs to be concatenated-2@|English
----------
     1  12345`34445`English@|1@|desc text-1@|1000@|English
     2  12345`34445`English@|2@|desc text-2@|1000@|English
----------
     1  12345`34445`Japanese@|1@|text-1@|WE@|PRT@|F@|500@|Japanese
     2  12345`34445`Japanese@|2@|text-2@|WE@|PRT@|F@|500@|Japanese
----------
$ cat script.pl
use warnings;
use strict;

## Check arguments. Only one input file.
@ARGV == 1 or die qq[Usage: perl $0 file\n];

## Open input file. End if error.
open my $ifh, "<", $ARGV[0] or die qq[Cannot open input file for reading: $!\n];

## Read first line and set the position to the beginning.
my $first_line = <$ifh>;
seek $ifh, 0, 0 or die qq[ERROR processing input file: $!\n];

## Select scenario based in number of columns:
## 4 -> First scenario
## 7 -> Second scenario.
## 5 -> Third scenario.
## 8 -> Fourth scenario.

## Sample input is not clear. I suppose there are no headers.

my @f = split /@\|/, $first_line;
if ( @f == 4 ) {
        process_scenario_1();
}
elsif ( @f == 7 ) {
        process_scenario_2();
}
elsif ( @f == 5 ) {
        process_scenario_3();
}
elsif ( @f == 8 ) {
        process_scenario_4();
}
else {
        die qq[ERROR: Incorrect format of input file\n];
}

sub process_scenario_1 {
        my (@f, %sequence);

        while ( <$ifh> ) {
                next if /^\s*$/;
                chomp;
                @f = split /@\|/;
                $sequence{ $f[1] } = $f[2];
        } continue {
                if ( eof ) {
                        printf  "%s\n", 
                                join( qq[@|], 
                                        $f[0], 
                                        join( qq[ ], map( $sequence{ $_ }, sort { $a <=> $b } keys %sequence ) ), 
                                        $f[-1] );
                }
        }
}

sub process_scenario_2 {
        my (@f, %desc, %sequence);

        while ( <$ifh> ) {
                next if /^\s*$/;
                chomp;
                @f = split /@\|/;
                push @{ $desc{ $f[1] } }, @f[2..4];
                $sequence{ $f[1] } = $f[5];
        } continue {
                if ( eof ) {
                        printf "%s\n",
                                join( qq[@|],
                                        $f[0],
                                        join( qq[@|], map( @{ $desc{ $_ } }, sort { $a <=> $b } keys %desc ) ),
                                        join( qq[ ], map( $sequence{ $_ }, sort { $a <=> $b } keys %sequence ) ),
                                        $f[-1] );
                }
        }
}

sub process_scenario_3 {
        my (@f, %sequence);

        while ( <$ifh> ) {
                next if /^\s*$/;
                chomp;
                @f = split /@\|/;
                $sequence{ $f[1] } = $f[2];
        } continue {
                if ( eof ) {
                        printf  "%s\n", 
                                join( qq[@|], 
                                        $f[0], 
                                        join( qq[ ], map( $sequence{ $_ }, sort { $a <=> $b } keys %sequence ) ), 
                                        @f[-2,-1] );
                }
        }
}

sub process_scenario_4 {
        my (@f, %sequence);

        while ( <$ifh> ) {
                next if /^\s*$/;
                chomp;
                @f = split /@\|/;
                $sequence{ $f[1] } = $f[2];
        } continue {
                if ( eof ) {
                        printf  "%s\n", 
                                join( qq[@|], 
                                        $f[0], 
                                        @f[3..5],
                                        join( qq[ ], map( $sequence{ $_ }, sort { $a <=> $b } keys %sequence ) ), 
                                        @f[-2,-1] );
                }
        }

}
$ perl script.pl
Usage: perl script.pl file
$ for file in scenario[1-4]; do printf "%s\n" "Command -> perl script.pl $file"; printf "%s\n" "Output ->"; perl script.pl "$file"; done 
Command -> perl script.pl scenario1
Output ->
12345`English@|perl is a scripting language Regular expression support is vast Has various items xml support is good@|English
Command -> perl script.pl scenario2
Output ->
12345`English@|DC@|34@|4456@|FC@|36@|4455@|BC@|39@|4453@|Text needs to be concatenated Text needs to be concatenated-1 Text needs to be concatenated-2@|English
Command -> perl script.pl scenario3
Output ->
12345`34445`English@|desc text-1 desc text-2@|1000@|English
Command -> perl script.pl scenario4
Output ->
12345`34445`Japanese@|WE@|PRT@|F@|text-1 text-2@|500@|Japanese

Regards,
Birei
Hi,

Thanks for the reply,

Is it possible to club all the scenario and produce one code.

I am getting confused. The goal is to have one program so only file name will be passed as an input (different scenarios)

For example:
Code:
Perl concat.pl file1.txt (file1.txt can be any scenario).

How to club all this into single instead of sub process_scenario_(1..n) have one subroutine which takes all scenarios.




Regards
Vanitha
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenate Logs - Perl Question

Hi All, I am fresh to perl and had been using shell scripting in my past experiences. In my part of perl program, i am trying to run a application command ccm stop, which should give some string output as the result. The output (error or sucess) has to be returned to an exisiting log file.... (4 Replies)
Discussion started by: ganga.dharan
4 Replies

2. Shell Programming and Scripting

Two Huge Texts and Combine Result to Third

hi, i want to examine two file and write some codes to a third file. note that seperators are TAB, not space. first file: 192.168.1.1 3 192.168.1.2 2 192.168.3.2 2 192.168.7.3 1 ... second file: 192.168.1.1 1 10.15.1.1 3 30 10.15.2.1 2 40 192.168.1.1 2 10.23.4.5... (3 Replies)
Discussion started by: gc_sw
3 Replies

3. Shell Programming and Scripting

extract texts using awk

Hello, I have two files: File1: a b c d File2: b c e I need 'e' as output.... Thanks.. ---------- Post updated at 12:16 PM ---------- Previous update was at 12:15 PM ---------- (1 Reply)
Discussion started by: shekhar2010us
1 Replies

4. Shell Programming and Scripting

Perl concatenate an array

Not a perl guru and need some help with a script I inherited. My perl script has a variable that is concatenated and works fine as is, but what I need is to remove a string in the output and input a files content. This is emailed as a html report and I can't get the file to output in the email... (5 Replies)
Discussion started by: numele
5 Replies

5. UNIX for Dummies Questions & Answers

perl filter unique and concatenate

Hi experts, I have some input like below, TEST A function W TEST A function X TEST B function Y TEST C function Z TEST C function ZY i would like to have below output, TEST A function W&X TEST B function Y TEST C function Z&ZY Please kindly help on this, i am cracking my head... (2 Replies)
Discussion started by: mingfatty
2 Replies

6. UNIX for Dummies Questions & Answers

Texts between 2 strings

Hi, I have a file with texts shown below, <2013 abc <2013 start request pdu dot1q end pdu response pdu dot1q end pdu am searching for the text "dot1q" , when it matches in the file , i need the contents between "<2013 start" and "end pdu". Can some one help on this ? ... (5 Replies)
Discussion started by: giri_luck
5 Replies

7. Shell Programming and Scripting

Concatenate small line with next line perl script

Hello to all, I'm new to perl, I have input file that contains the string below: 315350535ff450000014534130101ff4500ff45453779ff450ff45545f01ff45ff453245341ff4500000545000This string has as line separator "ff45". So, I want to print each line but the code below is not working. perl -pe '... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

8. Post Here to Contact Site Administrators and Moderators

Can there be alt-texts to icons?

Can you add hover texts (alt-texts) to icons? It is not always obvious what each of them mean just by looking at them. Sometimes it is clear from the url it points to, but for accessibility reasons alone it would be good to have alt-texts as a standard. (1 Reply)
Discussion started by: figaro
1 Replies

9. Shell Programming and Scripting

Aligning Texts

is there any way to align my text so every column begins on the same line as the previous line? here's my command: printf "THEN ( ${SEARCHPATTB} = Hour = ${CALTOTB} ) %8s => %8s NOW ( ${SEARCHPATT} = Hour = ${CALTOT} ) %7s => %7s Reduced By: %7s -${RESULT}"\\n output i'm currently getting... (2 Replies)
Discussion started by: SkySmart
2 Replies
All times are GMT -4. The time now is 03:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy