Solved

Perl print statement

Posted on 2009-03-30
30
271 Views
Last Modified: 2013-12-26
I have a script that set an variable $Parent and then through menu's you can select to print a file to your home directory. It now just prints traces.txt. I would like it to include the variable value.

See the line " system("$CAT_TRACE $file > $ENV{HOME}/tracesql.txt");" below in code

So if $PARENT=XOXOXO

Then the file name would be traces_XOXOXO.txt


# Copy trace file to user's homedirectory

sub copytrace()

{

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/tracesql.txt\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/tracesql.txt");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

}

Open in new window

0
Comment
Question by:bebo_c
  • 18
  • 10
  • +1
30 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24021123
system("$CAT_TRACE $file > $ENV{HOME}/tracesql_" . $PARENT . ".txt");

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24021176

system("$CAT_TRACE $file > $ENV{HOME}/traces_$PARENT.txt");

Open in new window

0
 

Author Comment

by:bebo_c
ID: 24021503
I changed but not quite what I need:

The $PARENT is a directory structure,,,I forgot:

$PARENT = /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS

See snippet below

I get:

Copy Trace file to home directory:
0. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/BAHRAMM0831_fbo-exc-boothg.kc.kingcounty.lcl.tracesql
1. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.xxx.lcl.tracesql
2. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/PS_fbo-exc-chor.xxxlcl.tracesql
Choose file (# or q to quit): > 2
Copying file /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/PS_fbo-exc-chor.xxx.lcl.tracesql to /home/chor/tracesql_/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS.txt
Are you sure? (y or n) > y
sh: /home/chor/tracesql_/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS.txt: Cannot create the specified file.
error! file not copied.
-------------------------
<Press ENTER to continue>




This part needs to be shorter:

sh: /home/chor/tracesql_/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS.txt

would like

 /home/chor/traces_HDEV89.txt
{

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/tracesql_". $PARENT .".txt\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/tracesql_". $PARENT .".txt");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24021939

...

return if (!&areyousure());  #EXISTING

my $ParentShort = $PARENT;   #NEW

$ParentShort =~ s|.*/(.*?)/LOGS.txt$|$1|;   #NEW

system("$CAT_TRACE $file > $ENV{HOME}/traces_$ParentShort.txt");   #CHANGED

if ($? > 0) { print "error! file not copied.\n"; }  #EXISTING

...

Open in new window

0
 

Author Comment

by:bebo_c
ID: 24022127
Still getting this:
Copy Trace file to home directory:
0. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/BAHRAMM0831_fbo-exc-boothg.kc.kingcounty.lcl.tracesql
1. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql
2. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/PS_fbo-exc-chor.kc.kingcounty.lcl.tracesql
Choose file (# or q to quit): > 1
Copying file /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql to /home/chor//apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql
Are you sure? (y or n) > y
sh: /home/chor/traces_/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS.txt: Cannot create the specified file.
error! file not copied.
-------------------------
<Press ENTER to continue>

sub lstrace()

{

        print "List of Trace files:\n";

        foreach my $file(&gettrace)

        {

                print "$file\n";

        }

        &pressenter();

}
 

# Copy trace file to user's homedirectory

sub copytrace()

{

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/$file\n";

        return if (!&areyousure());

        my $ParentShort = $PARENT;

        $ParentShort =~ s|.*/(.*?)/LOGS.txt$|$1|;

        system("$CAT_TRACE $file > $ENV{HOME}/traces_$ParentShort.txt");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

}

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24022484
Replace line 20 with this:

$ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

Open in new window

0
 

Author Comment

by:bebo_c
ID: 24022987
OK,

Almost perfect:

I now get a file called /hom/chor/HDEV89

You must be pulling that from $PARENT = /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql

Copy Trace file to home directory:
0. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/BAHRAMM0831_fbo-exc-boothg.kc.kingcounty.lcl.tracesql
1. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql
2. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/PS_fbo-exc-chor.kc.kingcounty.lcl.tracesql
Choose file (# or q to quit): > 1
Copying file /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql to /home/chor/HDEV89
Are you sure? (y or n) > y
File copied.
-------------------------
<Press ENTER to continue>

==================================================
Is there a way I can just get the filename?i.e. /home/chor/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql

 I do not understand your syntax "~ s|.*/(.*?)/[^/]+?$|$1|;"

Where can I change the value on above to get the file name?
# Copy trace file to user's homedirectory

sub copytrace()

{

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        my $ParentShort = $PARENT;

        $ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/$ParentShort\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/$ParentShort");

        if ($? > 0) { print "error! file not copied."; }

        else { print "File copied.\n"; }

        &pressenter();

}

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24023510
What this means:
    $ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

    =~       Regular expression
    s|         substitution
    .*         match anything, as much as possible
    /          match a "/"
    (.*?)     match anything, as little as possible, save to $1
    /          match a "/"
    [^/]+?   match anything not a "/", as little as possible
    $         end of string
    |$1|      replace everything found with what was in parenthesis


So, if $PARENT = '/home/chor/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql', what part would you want?
0
 

Author Comment

by:bebo_c
ID: 24023752
So actually $PARENT =  

/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql

If we want to write:

/home/chor/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql

We need to change:

  $ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

    =~       Regular expression
    s|         substitution
    .*         match anything, as much as possible
    /          match a "/"
    (.*?)     match anything, as little as possible, save to $1   <------------------This?
    /          match a "/"
    [^/]+?   match anything not a "/", as little as possible
    $         end of string
    |$1|      replace everything found with what was in parenthesis    <---------------And this?

I guess I still do not understand how you found and stored HDEV89
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24023812

$ParentShort =~ s|.*/||;  #Remove everything up to and including the last "/"

system("$CAT_TRACE $file > $ENV{HOME}/$ParentShort");

Open in new window

0
 

Author Comment

by:bebo_c
ID: 24023973
Oh, I see...We are just looking for matches......and the only two things that match is HDEV89 an "/"

How can we cut off ( the end of the string:GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql)

Can we set "/" as a delimiter and grab the last field? Not sure about the syntax.....
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 24024135
$PARENT = '/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql';
$delimiter="/";
$lastfield = (split/\Q$delimiter\E/,$PARENT)[-1];

0
 

Author Comment

by:bebo_c
ID: 24024147
This does not seem to work: So close though....

Copy Trace file to home directory:
0. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/BAHRAMM0831_fbo-exc-boothg.kc.kingcounty.lcl.tracesql
1. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql
2. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/JACOBSM0331_fbo-bhiptemp.kc.kingcounty.lcl.tracesql
3. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/PS_fbo-exc-chor.kc.kingcounty.lcl.tracesql
Choose file (# or q to quit): > 1
Copying file /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql to /home/chor/LOGS
Are you sure? (y or n) >


It's grabbing LOGS
# Copy trace file to user's homedirectory

sub copytrace()

{
 

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        my $ParentShort = $PARENT;

        #$ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

        $ParentShort =~ s|.*/||;

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/$ParentShort\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/$ParentShort");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

}

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24024230
What is $ENV{HOME}?  Either what I or ozo posted would get the stuff after the last "/".  
0
 

Author Comment

by:bebo_c
ID: 24024239
HOME=/home/chor
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:bebo_c
ID: 24024240
HOME=/home/chor
0
 

Author Comment

by:bebo_c
ID: 24024259

echo $HOME

HOME=/home/chor

echo $PARENT

/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql

I just want " HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql

It is printing 'to /home/chor/LOGS"
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24024275
So you want the last 3 fields of $PARENT?
0
 

Author Comment

by:bebo_c
ID: 24024302
No just the last:

GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql
0
 

Author Comment

by:bebo_c
ID: 24024324
Sorry, I was in error previously.

It is printing to /home/chor/LOGS  now

I want it to print to the filename:

/home/chor/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql

Sorry for the confusion
# Copy trace file to user's homedirectory

sub copytrace()

{
 

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        my $ParentShort = $PARENT;

        #$ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

        $ParentShort =~ s|.*/||;

        #$delimiter="/";

        #$lastfield = (split/\Q$delimiter\E/,$PARENT)[-1];

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/$ParentShort\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/$PARENT");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

}

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24024332
Both what I posted and what ozo posted will do that...

Can you post more of your script?  Are you sure $PARENT has what you expect?
##### The $PARENT value

$PARENT = '/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql';
 

##### What ozo posted

$delimiter="/";

$lastfield = (split/\Q$delimiter\E/,$PARENT)[-1];

print "lastfield='$lastfield'\n";
 

##### What Adam314 posted

my $ParentShort = $PARENT;

$ParentShort =~ s|.*/||;

print "ParentShort='$ParentShort'\n";

Open in new window

0
 

Author Comment

by:bebo_c
ID: 24024336
Now I got this:

Choose file (# or q to quit): > 1
Copying file /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql to /home/chor/LOGS
Are you sure? (y or n) >


It creates a file called LOGS
0
 

Author Comment

by:bebo_c
ID: 24024362
Here we go. I appreciate your help.


All of it works fine except where I print a file to homedir
#!/usr/bin/perl

# dbadev group debugging, allows users to access /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS

#

# HISTORY

# 030509  marchuw Added enableuserview option # 030209  marchuw Created script

#                 "snapclone.sh WHISTLER-HPRO89 SHASTA-HPRO89CP"

# 032309  cranest Added "Choose environment variables (HTEST89 / HDEV89)" 

#

#

# This script assumes that user is in dbadev user group and/or the below # sudo commands will work.  Make sure user is added to dbadev group.

#

# Current sudo commands in sudoers file:

# %dbadev  PS = (psmgr) NOPASSWD: /usr/bin/cat /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/*.tracesql, /usr/bin/ls -1 /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/, /usr/bin/rm -f /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/*.tracesql, /usr/bin/tail -f /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/*.tracesql, /usr/bin/tail /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/*.tracesql

#

# Outline of below: 

#

# List trace files

# Copy trace files

# RM trace files

# Tail trace files
 

# Main sudo variables

$PARENT="/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS";

$SUDO="/usr/bin/sudo -u psmgr";

$LIST_TRACE="$SUDO /usr/bin/ls -1 $PARENT/"; $CAT_TRACE="$SUDO /usr/bin/cat"; $RM_TRACE="$SUDO /usr/bin/rm -f"; $TAIL_TRACE="$SUDO /usr/bin/tail"; $TAILF_TRACE="$SUDO /usr/bin/tail -f";
 

# script variables

$USER=getpwuid($<);

$ENABLEUSERVIEW=0;
 

# Choose environment that user wants to run commands against (HDEV89 or HTEST89)
 

system("clear");

my $PARENT1="/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS";

my $PARENT2="/apps/psoft/hr890/HTEST89/appserv/HTEST89/LOGS";

my $PARENT='';

while(!$PARENT) {

	print "                         \n";

	print "                         \n";

	print "Please choose environment\n";

	print "                         \n";

	print "Press 1 for 'HDEV89'\n";

	print "Press 2 for 'HTEST89'\n";

	my $res=<STDIN>;

	chomp($res);

		if   ($res eq '1') {$PARENT = $PARENT1;}

		elsif($res eq '2') {$PARENT = $PARENT2;} }
 

# Then return to application and run commands
 
 

# Main loop

while(1)

{

	my $opt = &printmenu; chomp($opt);

	if    ($opt eq '0') { &lstrace(); }

	elsif ($opt eq '1') { &copytrace(); }

	elsif ($opt eq '2') { &rmtrace(); }

	elsif ($opt eq '3') { &tailtrace(); }

	elsif ($opt eq 'u') { &enableuserview(); }

	elsif ($opt eq 'q') { exit(0); }

	else 		    { print "Invalid option!\n\n"; &pressenter(); }

}
 

#prints main menu

sub printmenu()

{

	system("clear");

	print "--------------------------------\n";
 

	print "- DBA Dev log files "; 

	print "- listing files only for: $USER " if ($ENABLEUSERVIEW);

	print "\n";
 

	print "--------------------------------\n";

	print "0. List tracesql files\n";

	print "1. Copy tracesql file to HOME.\n";

	print "2. Remove tracesql file\n";

	print "3. Tail tracesql file\n";

	if (!$ENABLEUSERVIEW)

	{

		print "u. Option: list only files for current user: $USER\n\n";

	}

	else

	{

		print "u. Option: list files for all users\n\n";

	}

	print "Choose option (# or q to quit): > ";	

	return(<STDIN>);

}
 

# prompts user for file

# returns user chosen file

sub choosetrace()

{

	my @files = &gettrace();

	my %filelist;

	my $i=0;

	foreach (@files)

	{

		$filelist{$i} = $_;

		$i++;

	}

	while(1)	

	{

		my $opt;

		foreach (sort keys %filelist)

		{

			print "$_. $filelist{$_}\n";

		}

		print "Choose file (# or q to quit): > "; 

		$opt = <STDIN>; chomp($opt);

		return(0) if ($opt eq 'q');

		return($filelist{$opt}) if ($filelist{$opt});

	}

}
 

# List trace files

sub lstrace()

{

	print "List of Trace files:\n";

	foreach my $file(&gettrace)

	{

		print "$file\n";

	}

	&pressenter();

}
 

# Copy trace file to user's homedirectory sub copytrace() {
 

	print "Copy Trace file to home directory:\n";

	my $file = &choosetrace();

	my $ParentShort = $PARENT;

	#$ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

	$ParentShort =~ s|.*/||;

	#$delimiter="/";

	#$lastfield = (split/\Q$delimiter\E/,$PARENT)[-1];

	return if (!$file);

	print "Copying file $file to $ENV{HOME}/$ParentShort\n";

	return if (!&areyousure());

	system("$CAT_TRACE $file > $ENV{HOME}/$ParentShort");

	if ($? > 0) { print "error! file not copied.\n"; }

	else { print "File copied.\n"; }

	&pressenter();

}
 

# rm trace file

sub rmtrace()

{

	print "Remove Trace file:\n";

	my $file = &choosetrace();

	return if (!$file);

	print "Removing file $file\n";

	return if (!&areyousure());

	system("$RM_TRACE $file");

        if ($? > 0) { print "error! file not removed.\n"; }

        else { print "File removed.\n"; }

	&pressenter();

}
 

# tail trace file

sub tailtrace()

{

	my $type=1;

	print "Tail Trace file:\n";

	print "type of trace: \n";

	print "1. Quick tail(default).\n";

	print "2. Continuous tail.\n";

	print "Choose option (#): > ";

	my $opt = <STDIN>; chomp($opt);

	$type = 2 if ($opt eq '2');

	my $file = &choosetrace();

	return if (!$file);

	if ($type == 2) { system("$TAILF_TRACE $file"); }

	else		{ system("$TAIL_TRACE $file"); }

	&pressenter();

}
 

# confirm from user

sub areyousure()

{

	while (1)

	{

		print "Are you sure? (y or n) > ";

		my $opt = <STDIN>; chomp($opt);

		if ($opt eq 'y') { return(1); }

		elsif ($opt eq 'n') { return(0); }

	}

}
 

# pressenter

sub pressenter()

{

	print "-------------------------\n";

	print "<Press ENTER to continue>\n";

	<STDIN>;

}
 

# returns list of *.tracesql files

sub gettrace()

{

	my @files;

	foreach my $file (split(/\n/, `$LIST_TRACE`))

	{

		if ($file =~ /\.tracesql$/)

		{

			if ($ENABLEUSERVIEW) 

			{ 

				push(@files, "$PARENT/$file") if ($file =~ /$USER/i); 

			}

			else

			{

				push(@files, "$PARENT/$file");

			}

		}	

	}

	return(@files);

}
 

# enables user view

sub enableuserview()

{

	if ($ENABLEUSERVIEW)

	{

		$ENABLEUSERVIEW=0;

	}

	else

	{

		$ENABLEUSERVIEW=1;

	}

}

Open in new window

0
 

Author Comment

by:bebo_c
ID: 24031812
So where I am at now is:

Copy Trace file to home directory:
0. /apps/psoft/hr890/HTEST89/appserv/HTEST89/LOGS/BAHRAMM0831_fbo-exc-boothg.kc.kingcounty.lcl.tracesql
1. /apps/psoft/hr890/HTEST89/appserv/HTEST89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql
2. /apps/psoft/hr890/HTEST89/appserv/HTEST89/LOGS/JACOBSM0331_fbo-bhiptemp.kc.kingcounty.lcl.tracesql
3. /apps/psoft/hr890/HTEST89/appserv/HTEST89/LOGS/PS_fbo-exc-chor.kc.kingcounty.lcl.tracesql
Choose file (# or q to quit): > 3
Copying file /apps/psoft/hr890/HTEST89/appserv/HTEST89/LOGS/PS_fbo-exc-chor.kc.kingcounty.lcl.tracesql to /home/chor/LOGS
Are you sure? (y or n) >

I prints the file /home/chor/LOGS

I want it it print: /home/chor/PS_fbo-exc-chor.kc.kingcounty.lcl.tracesql

It is the same content, it is just pulling 1 delimiter "/LOGS" before the filename /PS_fbo-exc-chor.kc.kingcounty.lcl.tracesql

I have tried using:

$delimiter="/";
$lastfield = (split/\Q$delimiter\E/,$PARENT)[-1];

It prints LOGS also instead of filename
# Copy trace file to user's homedirectory

sub copytrace()

{
 

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        my $ParentShort = $PARENT;

        $ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

        #$ParentShort =~ s|.*/||;

        $delimiter="/";

        $lastfield = (split/\Q$delimiter\E/,$PARENT)[-1];

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/$lastfield\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/$lastfield");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

Open in new window

0
 

Author Comment

by:bebo_c
ID: 24034490
Is it because there is no delimiter on the end so it cannot grab the last field?:

/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24088551
No, if there was a delimiter at the end, you would get empty.

What is the output you get from this script.
#!/usr/bin/perl
 

##### The $PARENT value

$PARENT = '/apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql';

 

##### What ozo posted

$delimiter="/";

$lastfield = (split/\Q$delimiter\E/,$PARENT)[-1];

print "lastfield='$lastfield'\n";

 

##### What Adam314 posted

my $ParentShort = $PARENT;

$ParentShort =~ s|.*/||;

print "ParentShort='$ParentShort'\n";

Open in new window

0
 

Author Comment

by:bebo_c
ID: 24088957
I get what you are saying. I got:
root> ./test.stv
lastfield='GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql'
ParentShort='GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql'



But my script still prints incorrectly. Is my syntax incorrect?


I get this output:
 

Choose option (# or q to quit): > 1

Copy Trace file to home directory:

0. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/BAHRAMM0831_fbo-exc-boothg.kc.kingcounty.lcl.tracesql

1. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/BERGMAF0222_hr249012.kc.kingcounty.lcl.tracesql

2. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql

3. /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/JACOBSM0331_fbo-bhiptemp.kc.kingcounty.lcl.tracesql

Choose file (# or q to quit): > 2

Copying file /apps/psoft/hr890/HDEV89/appserv/HDEV89/LOGS/GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql to /home/chor/LOGS

Are you sure? (y or n) >
 
 

From:
 

==========================Here=========================================

# Copy trace file to user's homedirectory

sub copytrace()

{
 

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        my $ParentShort = $PARENT;

        #$ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

        $ParentShort =~ s|.*/||;

        $delimiter="/";

        $lastfield = (split/\Q$delimiter\E/,$PARENT)[-1];

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/$lastfield\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/ParentShort='$ParentShort'\n");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

}
 
 

===================And here==========================

# Copy trace file to user's homedirectory

sub copytrace()

{
 

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        my $ParentShort = $PARENT;

        #$ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

        $ParentShort =~ s|.*/||;

        $delimiter="/";

        $lastfield = (split/\Q$delimiter\E/,$PARENT)[-1];

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/$lastfield\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/lastfield='$lastfield'\n");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

}

Open in new window

0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 75 total points
ID: 24089320
Are you sure $PARENT has what you believe?

Try this for the copytrace sub.  What is the output you get with this?
sub copytrace()

{

 

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        my $ParentShort = $PARENT;

        print "\n** In copytrace, \$PARENT='$PARENT'\n";

        $ParentShort =~ s|.*/||;

        print "** \$ParentShort='$ParentShort'\n";

        $delimiter="/";

        $lastfield = (split/\Q$delimiter\E/,$PARENT)[-1];

        print "** \$lastfield='$lastfield'\n";

        print "** \$ENV{HOME}='$ENV{HOME}'\n";

        print "\n";

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/$lastfield\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/lastfield='$lastfield'\n");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

}

Open in new window

0
 

Author Comment

by:bebo_c
ID: 24089519
IO must of had some bad syntax somewhere.
It works perfect now.
Sorry for the issues
Many thanks to the both of you
# Copy trace file to user's homedirectory

sub copytrace()

{
 

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        #my $ParentShort = $PARENT;

        #$ParentShort =~ s|.*/(.*?)/[^/]+?$|$1|;

        #$ParentShort =~ s|.*/||;

        $delimiter="/";

        $lastfield = (split/\Q$delimiter\E/,$file)[-1];

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/$lastfield\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/$lastfield\n");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

}

Open in new window

0
 

Accepted Solution

by:
bebo_c earned 0 total points
ID: 24089671
I cleaned up syntax and it works...

Many thanks...
# Copy trace file to user's homedirectory

sub copytrace()

{
 

        print "Copy Trace file to home directory:\n";

        my $file = &choosetrace();

        $delimiter="/";

        $lastfield = (split/\Q$delimiter\E/,$file)[-1];

        return if (!$file);

        print "Copying file $file to $ENV{HOME}/$lastfield\n";

        return if (!&areyousure());

        system("$CAT_TRACE $file > $ENV{HOME}/$lastfield\n");

        if ($? > 0) { print "error! file not copied.\n"; }

        else { print "File copied.\n"; }

        &pressenter();

}

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 94
not able to insert into temp table 68 150
Complete beginner needs help making a cron job 9 104
modThree challenge 4 64
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now