Errors in Perl when using awk command


 
Thread Tools Search this Thread
Top Forums Programming Errors in Perl when using awk command
# 1  
Old 01-02-2012
Errors in Perl when using awk command

Hi Guys,

Hope everyone is fine Smilie
I have this code below:
Code:
#!/usr/bin/perl

$num_of_files=`ls | grep -v remover | wc -l`;
$remover=`ls -lrt | grep -v total | grep -v remover | head -1 | awk '{print $8}' | rm \`xargs\``;

if ($num_of_files>3) {
        system ($remover);
}

When I tried to execute it, found an error which looks like this below:
Code:
rm: invalid option -- 'w'
Try `rm --help' for more information.

What is the meaning of this error and how can I make my script work? Please advise.


br,
rymnd_12345
# 2  
Old 01-02-2012
Given what you've shown, I'd say you just need to wrap your vars with single quotes when they're used; ie, '$var'. This may not be it, as it's balking at barewords, but it allows for the proper shell-out that you'd defined...but then again given what we've seen, if that's the extent of your script, why not just leave it as a shell script? Perl isn't even needed under the circumstances.
# 3  
Old 01-03-2012
Hi curleb,

Thanks for the info, for now I've already created one using shell script this time. The script is now working and later I will put it in CRON.

Our server can only accept CRON entries provided you're a root user, so I've tried to input the script as a root user, my problem is that it's not functioning. Here is the analogy below:

Files that I want to remove are located in /home/user/USER_LOGS/, the script is also located in this directory. This is the script:
Code:
#!/usr/bin/sh

num_of_files=`ls /home/nsadm/USER_LOGS/ | grep UserActivity | grep UMB00 | wc -l`

if [ $num_of_files -gt 3 ]
then
remove=`ls -lrt /home/nsadm/USER_LOGS/ | grep UserActivity | grep UMB00 | head -1 | awk '{print $8}' | rm \`xargs\``
$remove
fi

but as per running the script when I'm in the directory /root (this is the default directory upon login as root user), it can't see the file and therefor it can't perform the rm function. This is what I've did as root user:
Code:
root:~ # /home/user/USER_LOGS/remover.sh
rm: cannot remove `09:46': No such file or directory

I think the problem here is that it can't locate the files that I want to delete under the /root directory. Can you help me how to enhance my script so that if ever I would like to put it in CRON it will function normally, by removing file on top and retaining only 3 files on it.


Please advice,
rymnd_12345

Last edited by vbe; 01-04-2012 at 12:30 PM.. Reason: code tag correction...
# 4  
Old 01-03-2012
your file list includes some info from the long listing (ls -lrt) by accident ... confirm by running the code below by itself ...
Code:
ls -lrt /home/nsadm/USER_LOGS/ | grep UserActivity | grep UMB00 | head -1 | awk '{print $8}'

... use "ls -rt" instead of "ls -lrt" ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk command in Perl

Hi All, I got some requirement of using Awk in perl subroutine One of the variable in my perl script holds below type of data. I want to split this value as quotes seperated and get the second last field i.e., 3206000 from above. I used below code in my perl sub-routine,... (3 Replies)
Discussion started by: hi.villinda
3 Replies

2. Shell Programming and Scripting

Convert awk command to Perl

Hi all; I have a Perl script that uses this awk command wrapped in a "system" call; it works exactly as I want it; however I want to keep this a Perl script and have very few if any "system" command...so the question; can someone help me code this in Perl please ... i have tried an now I am... (1 Reply)
Discussion started by: gvolpini
1 Replies

3. Shell Programming and Scripting

using awk in perl with split command

Hi, I have an array with following data. First field shows the owner and second is unique name. Now i have to pic the latest value with respect to the date in case of duplicate. like "def" is from two owners "rahul/vineet", now i want the latest from the two and the owner name also for all the... (9 Replies)
Discussion started by: vineet.dhingra
9 Replies

4. Shell Programming and Scripting

usage of AWK command under perl script

i have two files as shown below t1.txt: argument1 argu2 argu37 t2.txt: 22 33 44 i want o/p as argument1 22 argu2 33 argu37 44 i am trying to merge two file under perl script using following system("paste t1.txt t2.txt | awk... (3 Replies)
Discussion started by: roopa
3 Replies

5. Programming

Executing a awk command inside perl script --

Hello experts I want to execute a awk command, which reads from txt files and sums the numbers from the first column for those listed only inside a <init> block -- The awk command is like awk '/<\/?init>/{x = !x}x{a++}x && a > 2{sum+=$1}END{printf"%E" "\n", sum} So, I want to execute... (2 Replies)
Discussion started by: Alkass
2 Replies

6. Shell Programming and Scripting

logging errors using perl

Hi, Can some one please tell me how to redirect only errors to the log file using perl. Thanks, Anjan. ---------- Post updated at 08:41 PM ---------- Previous update was at 08:39 PM ---------- Also, i dont want to use any module. Please tell me in normal way (1 Reply)
Discussion started by: Anjan1
1 Replies

7. Shell Programming and Scripting

Command to remove duplicate lines with perl,sed,awk

Input: hello hello hello hello monkey donkey hello hello drink dance drink Output should be: hello hello monkey donkey drink dance (9 Replies)
Discussion started by: cola
9 Replies

8. Shell Programming and Scripting

Perl: Backtick Errors

When trying to use backticks for system commands, is there a way to read the error messages if a command doesn't execute properly? I have no problem getting the results if the command is properly executed. Ex. my @result = `dir`; foreach my $line (@result) { print "Result = $line";... (2 Replies)
Discussion started by: kooshi
2 Replies

9. Shell Programming and Scripting

PERL Syntax Errors

Hi, I am a newbie to PERL and working on a script. When running it I get a lot of compilation errors. The actual command in the program (which is within a case structure) is given below # This gives the actual count of inquires from a log file (It works fine when I type this on the... (2 Replies)
Discussion started by: nurani
2 Replies

10. Shell Programming and Scripting

awk command with PERL

Hi All, My Input file looks like below: Input: 100,200,300 $fw=`head -1 test.csv | awk -F, '{print \$1}'`; $fw="'$fw" $fw="$fw'" print $fw Output:'100' I want the first field to be printed in single quotes ('') like above. I could get the ouptput but the problem is single... (6 Replies)
Discussion started by: kmkbuddy_1983
6 Replies
Login or Register to Ask a Question