LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2009, 11:25 AM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
Can awk be used to call shell programs (cat, cp, rm, etc.)


Hi all,

I am trying to write a simple awk script to remove a file (infile) if another file containing information about it (infile.count) contains a value >5 anywhere in $2.

Code:
awk -F" " '{if ($2>5) rm infile}' infile.count
When I run the script as is, nothing happens. When I change rm infile to print $2 it behaves normally. Is there a way to get awk to call rm or another shell program? Otherwise, can anyone give me me pointers in writing a script that removes infile if infile.count contains any integers greater than 5?

infile.count looks like this:
Code:
ACAL 6
APOM 29
OCT1 3
MGAL 2
CNIT 1
CAPI 1
NVEC 1
HROB 5
Thanks!
Kevin
 
Old 11-16-2009, 11:46 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
You can use awk's built-in function "system()". Here's an example from The GNU Awk User's Guide

For example, if the following fragment of code is put in your awk program:
Code:
END {
    system("date | mail -s 'awk run done' root")
}
the system administrator is sent mail when the awk program finishes processing input and begins its end-of-input processing.
 
Old 11-17-2009, 10:05 PM   #3
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
Thanks for the help but I'm still having a problem. For these examples I renamed my infile that I wanted to delete to lamb.txt to demonstrate a problem I'm having.

With quotes around the filename I get this (the file does exist):
Code:
awk -F " " '{if ($2>5) {system(rm "lamb.txt")}}' orthology_4_test.txt
sh: lamb.txt: not found
Without quotes around the filename I get this:
Code:
awk -F " " '{if ($2>5) {system(rm lamb.txt)}}' orthology_4_test.txt
awk: {if ($2>5) {system(rm lamb.txt)}}
awk:                           ^ syntax error
awk: fatal: 0 is invalid as number of arguments for system


Can you tell me what I'm doing wrong now? I can't find many examples using system but I assume rm is a legal program to call using it?

Thanks!
Kevin
 
Old 11-17-2009, 10:10 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
the error says it, do you actually have lamb.txt in the current path?
 
Old 11-18-2009, 12:53 AM   #5
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
Absolutely. I really don't understand what the problem is.
 
Old 11-18-2009, 01:14 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you have two $2 values that are more than 5. When the 2nd record that has $2 > 5 is processed, the lamb.txt is already deleted. that's why it cannot find the file!
 
Old 11-19-2009, 01:37 AM   #7
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
I neglected to mention the fact that the file isn't actually being deleted.
 
Old 11-19-2009, 05:06 AM   #8
Vrajgh
Member
 
Registered: Aug 2005
Posts: 65

Rep: Reputation: 31
Try:
Code:
$ awk -F " " '{if ($2>5) {system("rm lamb.txt")}}'
 
Old 11-19-2009, 06:16 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by kmkocot View Post
With quotes around the filename I get this (the file does exist):
Code:
awk -F " " '{if ($2>5) {system(rm "lamb.txt")}}' orthology_4_test.txt
sh: lamb.txt: not found
In this case the string passed to the system function is the awk expression rm "lamb.txt". The variable rm is not set so the expression is evaluated to lamb.txt and the result is as if you had entered that at the prompt.

Quote:
Originally Posted by kmkocot View Post
Without quotes around the filename I get this:
Code:
awk -F " " '{if ($2>5) {system(rm lamb.txt)}}' orthology_4_test.txt
awk: {if ($2>5) {system(rm lamb.txt)}}
awk:                           ^ syntax error
awk: fatal: 0 is invalid as number of arguments for system
In this case the string passed to the system function is the awk expression rm lamb.txt. The . in lamb.txt is not syntactically valid.

Vrajgh's suggestion should work.
 
Old 11-19-2009, 07:05 AM   #10
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS6
Posts: 267

Rep: Reputation: 58
You are actually setting up the command line in awk and THEN executing it. Try:
Code:
{print"/bin/rm","lamb.txt"; system("")}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cat as a login shell b8rt Linux - Security 7 01-21-2012 10:42 AM
cat command space problem with awk in for loop. shan_nathan Programming 3 06-18-2009 06:51 AM
cat|grep|awk pudhiyavan Programming 6 07-14-2008 02:56 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
How to acess Variable defined in perl script inside an awk call sumin Programming 3 04-26-2007 06:19 AM


All times are GMT -5. The time now is 11:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration