• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Perl print statement

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
bebo_c
Asked:
bebo_c
  • 18
  • 10
  • +1
3 Solutions
 
mrjoltcolaCommented:
system("$CAT_TRACE $file > $ENV{HOME}/tracesql_" . $PARENT . ".txt");

0
 
Adam314Commented:

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

Open in new window

0
 
bebo_cAuthor Commented:
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
Adam314Commented:

...
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
 
bebo_cAuthor Commented:
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
 
Adam314Commented:
Replace line 20 with this:

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

Open in new window

0
 
bebo_cAuthor Commented:
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
 
Adam314Commented:
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
 
bebo_cAuthor Commented:
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
 
Adam314Commented:

$ParentShort =~ s|.*/||;  #Remove everything up to and including the last "/"
system("$CAT_TRACE $file > $ENV{HOME}/$ParentShort");

Open in new window

0
 
bebo_cAuthor Commented:
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
 
ozoCommented:
$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
 
bebo_cAuthor Commented:
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
 
Adam314Commented:
What is $ENV{HOME}?  Either what I or ozo posted would get the stuff after the last "/".  
0
 
bebo_cAuthor Commented:
HOME=/home/chor
0
 
bebo_cAuthor Commented:
HOME=/home/chor
0
 
bebo_cAuthor Commented:

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
 
Adam314Commented:
So you want the last 3 fields of $PARENT?
0
 
bebo_cAuthor Commented:
No just the last:

GROESBD0514_fbod-2-groesbd.kc.kingcounty.lcl.tracesql
0
 
bebo_cAuthor Commented:
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
 
Adam314Commented:
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
 
bebo_cAuthor Commented:
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
 
bebo_cAuthor Commented:
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
 
bebo_cAuthor Commented:
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
 
bebo_cAuthor Commented:
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
 
Adam314Commented:
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
 
bebo_cAuthor Commented:
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
 
Adam314Commented:
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
 
bebo_cAuthor Commented:
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
 
bebo_cAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 18
  • 10
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now