Can awk be used to call shell programs (cat, cp, rm, etc.)
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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!
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.