Computing.Net > Forums > Unix > awk command

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

awk command

Reply to Message Icon

Name: ephaguilon
Date: May 27, 2005 at 03:18:23 Pacific
OS: Unix
CPU/Ram: server
Comment:

Hi

Can anyone help is there a command in Unix in side the awk function which i can check if the file exist or a dierctory does exist?.


regards,
Raim



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: May 27, 2005 at 06:40:58 Pacific
Reply:

awk uses the system function to execute any OS command, and the return code can be captured and checked, as shown below. You can also read in all the output of a system call (very handy).

When I check the code returned from the OS, zero means success. But within awk, a zero expression means false and non-zero means true. If you run the code below, you will see that "one means true".

awk 'BEGIN {

rc=system("test -f myfile")
if (rc==0)
   print "exists"
else
   print "does NOT exist"

if (system("test -f myfile"))
   print "does NOT exist"
else
   print "exists"

if (0)
   print "zero means true"

if (1)
   print "one means true"

exit}'


0

Response Number 2
Name: WilliamRobertson
Date: May 28, 2005 at 06:06:47 Pacific
Reply:

Cool - never tried that.

So to test for a file's existence, one way would be:

awk 'BEGIN {
if (! system("test -f myfile")) print "myfile exists"
else print "myfile does not exist"
}'



0

Response Number 3
Name: vibhor_agarwalin
Date: May 30, 2005 at 04:24:29 Pacific
Reply:

Great,

I was wondering the same thing a couple of days earlier.

Can you give me a simple eg as to how to execute simple commands inside awk like ls,echo,etc.
I am pretty confused about that.

I don't thing they need the system call.

Thanks


0

Response Number 4
Name: Jim Boothe
Date: May 31, 2005 at 06:38:12 Pacific
Reply:

I use the system call when I want to get the success code returned to me. And on that system call, you probably want to control where you send stdout and stderr.

When I want the output of the command returned to me, I use the following construct:

"ls -l file1" | getline
printf "file size is %d bytes\n",$5

The above getline goes into $0. Or I can read the output into a named variable:

"echo $HOME" | getline myhome
print myhome


0

Response Number 5
Name: ephaguilon
Date: May 31, 2005 at 20:27:31 Pacific
Reply:

It works Thanks a lot

regards,
Raim


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: awk command

How to use AWK command www.computing.net/answers/unix/how-to-use-awk-command/6686.html

shortcut - awk command www.computing.net/answers/unix/shortcut-awk-command/4271.html

AWK command to find 2 values in a f www.computing.net/answers/unix/awk-command-to-find-2-values-in-a-f/5853.html