Parse through a txt file PERL scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse through a txt file PERL scripting
# 1  
Old 05-12-2016
Parse through a txt file PERL scripting

Below is a perl code I am trying.

Code:
#!/usr/bin/perl

#use strict;
use warnings qw/ all FATAL /;

use constant ENV_FILE => '/apps/env_data.txt';

$uenv = $ARGV[0];

my $input = $uenv;


open my $fh, '<', ENV_FILE
    or die sprintf qq{Unable to open "%s" for input: $!}, ENV_FILE;

LINE:
while ( <$fh> ) {
    next unless /\S/;
    my ($env, $dir, $servers) = split;
    next unless $env eq $input;

    for my $server ( split /,/, $servers ) {

        ## Connect to $server and access $dir
        system "ssh $server 'cat $dir/server.properties'";

        last LINE;
    }
}

Code:
cat /apps/env_data.txt

qa1 /apps/8801/conf qavm01,qavm02,qavm03
qa2 /apps/8901/conf qavm01,qavm03,qavm04
qa3 /apps/8701/conf qavm01,qavm04,qavm02
cpc /apps/19100/conf cpcint101
cpc2 /apps/19101/conf cpcint02

for some reason the script is connecting to only $server out of the $servers to display the value. Any thoughts on it? I am still learning perl. So I might be making silly mistakes.
# 2  
Old 05-12-2016
remove line
Code:
last LINE;

and try .
# 3  
Old 05-12-2016
A bit simpler?

Code:
#!/usr/bin/env perl
#
use strict;
use warnings;

my $env_file = '/apps/env_data.txt';
my $uenv = shift or die "Missing uenv parameter";

open my $fh, '<', $env_file or die "Unable to open $env_file: $!";

while(<$fh>) {
    if(/^$uenv\s/) {
        my ($env, $dir, $servers) = split;
        for my $server (split /,/, $servers) {
            system "ssh $server 'cat $dir/server.properties'";
        }
        last;
    }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Scripting - parse file

Test jython (3 Replies)
Discussion started by: morpheus59
3 Replies

2. Shell Programming and Scripting

awk or perl to parse file

I have an input file attached that I am trying to parse in tab-delimanted format: The chromosomal variant column contains all the information: parse rules: 1. 4 zeros after the NC_ and the digits before the . 2. digits after the g. repeated twice separated by a tab 3. letter before the > 4.... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

Delete file2.txt from file1.txt using scripting

Hi, I`m a total newbie, well my requirement is that i have 2 files I want to identify which countries i do not currently have in db.. how can i use the grep or another command to find this file .. i want to match all-countries.txt with countries-in-db.txt so the output is equal to... (11 Replies)
Discussion started by: beanbaby
11 Replies

4. Shell Programming and Scripting

RegeX to parse data from a txt file

Hi all the experts out there, I am totally new to perl and I was given an assignment by using Perl to find the 2nd element of each line in each curly bracket which made up of 5 elements. Expected result should like this: Type: VCC Pin_name: AK32,AL32,AH21,..... Type: NC Pin_name:... (2 Replies)
Discussion started by: killbanne
2 Replies

5. Shell Programming and Scripting

Perl: Parse Hex file into fields

Hi, I want to split/parse certain bits of the hex data into another field. Example: Input data is Word1: 4f72abfd Output: Parse bits (5 to 0) into field word1data1=0x00cd=205 decimal Parse bits (7 to 6) into field word1data2=0x000c=12 decimal etc. Word2: efff3d02 Parse bits (13 to... (1 Reply)
Discussion started by: morrbie
1 Replies

6. Shell Programming and Scripting

Parse file contents in perl...

Hi, I have the file like this: #Contents of file 1 are: Dec 10 12:33:44 User1 Interface: Probe Dec 10 12:33:47 uSER1 SOME DATA Dec 10 12:33:47 user1 Interface: MSGETYPE Dec 10 12:34:48 user1 ID: 10. Dec 10 12:33:55 user1 Interface: MSGTYPE Dec 10 12:33:55 user1 Id: 9 ... (1 Reply)
Discussion started by: vanitham
1 Replies

7. Shell Programming and Scripting

PERL:How to convert numeric values txt file to PACKED DECIMAL File?

Is there any way to convert numeric values txt file to PACKED DECIMAL File using PERL. Regards, Alok (1 Reply)
Discussion started by: aloktiwary
1 Replies

8. UNIX for Dummies Questions & Answers

How to cut data block from .txt file in shell scripting

Hi All, Currently i have to write a script. For which i need to cut a block from .txt file. I know the specific word that starts the block and ends the block. Can we do it in shell scripting..? Please suggest.... (6 Replies)
Discussion started by: pank29
6 Replies

9. Shell Programming and Scripting

CSV File parse help in Perl

Folks, I have a bit of an issue trying to obtain some data from a csv file using PERL. I can sort the file and remove any duplicates leaving only 4 or 5 rows containing data. My problem is that the data contained in the original file contains a lot more columns and when I try ro run this script... (13 Replies)
Discussion started by: lodey
13 Replies

10. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies
Login or Register to Ask a Question