CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Stupid question from a beginner?

 



Bill
New User

Nov 4, 2002, 2:03 PM

Post #1 of 3 (693 views)
Stupid question from a beginner? Can't Post

Hi All,

I'm (extremely new to perl) and forgive me for asking a silly question! I've been trying to figure this one out for over 10 hours now, and to be honest I would very much appreciate any advice/pointers. I have to take some UNIX Shell scripts and convert them into Perl scripts, which will run in a windows NT environment, and I have to the end of the week (to find another job :-).

I'm okayish, with the basics, but have struggled to figure out the below,

# Start of text file

0783889, 5765-B75 (C) Copyright IBM Corp. 1994, 2000. ALL RIGHTS RESERVED.
Starting MQSeries Commands.

1 : DISPLAY channel(*)
CHANNEL(XXXXXXXXXXXXXXXXXX) CHLTYPE(SDR)
AMQ8414: Display Channel details.
CHANNEL(XXXXXXXXXXXXXXXXXX) CHLTYPE(SDR)
AMQ8414: Display Channel details.
CHANNEL(XXXXXXXXXXXXXXXXXX) CHLTYPE(SDR)
AMQ8414: Display Channel details.
CHANNEL(SYSTEM.ADMIN.SVRCONN) CHLTYPE(SVRCONN)

2 : END
One MQSC command read.
No commands have a syntax error.
All valid MQSC commands were processed.

# End of text file

To achieve the desired result and setting up a variable which would be used later in the script with a for statement the following is ran;

CHANNELS=`grep CHANNEL postingfile.txt | grep -v SYSTEM | awk -F'(' '{print $2}' | awk -F')' '{print $1}`

# Desired result returned
# echo $CHANNELS
XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX

Embarrassingly, I've only managed to figure out the following and probably have this wrong to; I'm trying to get what is in between the parenthesis of the first column in the file and store this in a variable:

open MQOUTPUT, "<postingfile.txt" or warn "Can't open $input: $!\n";
@temp1 = grep /channel/i, <MQOUTPUT>;
close(MQOUTPUT) or warn "Error - Unable to close file $input $!\n";
print @CHANNELS = grep !/system|^A|:/i, @temp1;

CHANNEL(XXXXXXXXXXXXXXXXXX) CHLTYPE(SDR)
CHANNEL(XXXXXXXXXXXXXXXXXX) CHLTYPE(SDR)
CHANNEL(XXXXXXXXXXXXXXXXXX) CHLTYPE(SDR)

I would really appreciate any advice/help and sorry for going on.....

Regards

Bill


Kanji
User / Moderator

Nov 5, 2002, 12:09 AM

Post #2 of 3 (686 views)
Re: [Bill] Stupid question from a beginner? [In reply to] Can't Post


Quote
grep CHANNEL postingfile.txt | grep -v SYSTEM | awk -F'(' '{print $2}' | awk -F')' '{print $1}`


It probably would have helped your Perl porting efforts if whomever wrote the above had done so entirely with awk ...


Code
awk 'BEGIN { FS="[()]" } /CHANNEL/ && !/SYSTEM/ { print $2 }' postingfile.txt


... which would translate to something like ...


Code
my @channels = (); 

while ( <MQOUTPUT> ) {
next unless /CHANNEL/ && !/SYSTEM/;
my @field = split /[()]/;
push @channels, $field[1];
}


... where @channels will now contain all your non-SYSTEM channels.

There are other ways of doing this with grep, like you started to, but if you're a newbie, they're probably down a road you don't yet want to find yourself travelling.

On a side note, have you looked at [url=http://www.cygwin.com/]Cygwin? It gives you a UNIX-like environment within Windows, and comes complete with many of the shell tools you'd expect to find (grep and awk included), saving you from having to convert your shell scripts in the first place.
--k.


Bill
New User

Nov 5, 2002, 2:45 AM

Post #3 of 3 (681 views)
Re: [Kanji] Stupid question from a beginner? [In reply to] Can't Post

Hi --K,

Many thanks for your response, this is exactly what I was trying to achieve! I never considered Cygwin, although I suspect with time constraints and these being high-end production boxes, I wouldn't be allowed to install the software. This would be an ideal solution for me. All the production boxes come with Perl and it is the only alterative at the moment. I'm sure I'll get there in the end :-)

I think I'll take your sound advice and stay away from the grep function in perl for the time being, until I get a better grip on the basics....

Regards

Bill
Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives