awk command to test if a string is a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to test if a string is a file
# 1  
Old 10-14-2009
awk command to test if a string is a file

What awk command will test a string to determine if it is a valid file name?

With the following awk statement I isolate the lines from the inputfile that might contain a filename, then I attempt to test the possible filename which is always in $4 from each line. However it is not working at all as i do not think the system command gets passed $4 from awk. Have been struggling with this all day

Quote:
if ( $1 ~ /^$/ )
then
system("ls $4;echo $?")
{ print $0
# 2  
Old 10-15-2009
Should be something like this:

Code:
if (system("test -r " $4)){
  print $4 " not found"
}
else {
  print $4 " found"
}

# 3  
Old 10-15-2009
Hi Arsenalman!

First of all:
Quote:
Originally Posted by Arsenalman
if ( $1 ~ /^$/ )
means empty string (literally line which has beginning and end and nothing between them).
Then 'then'. I usually don't use it in awk scripts. My awk gives no error if I put it after 'if'-statement, but I'm not sure that its behavior is correct.
Now your system call:
Quote:
Originally Posted by Arsenalman
system("ls $4;echo $?")
{ print $0
Obviously you meant, that 'system' will invoke 'ls $4', print the exit code of the command back to awk script and then awk will print it with 'print $0'. It doesn't work such way. :-)
'system' simply calls a system command and does not return the output of the command to awk. What it returns is....I'll give you three guesses... Yeahh.. The exit code of the invoked command.
so
Code:
exit_code=system("ls " $4)

would do in your case, but it is better to use a special shell command which checks whether its argument is a file: 'test -f'. It returns 1 if the tested thing is not a file, and 0 if it is, so we have
Code:
exit_code=system("test -f " $4)

now we need to check what the exit code was
Code:
exit_code=system("test -f " $4)
if (exit_code) # condition in brackets which isn't 0 or Empty means TRUE
                   # in our case exit_code would be 1 if file does not exist
   print $4 " is not a file"
else
   print $4 " is a file"

You can do it even smarter. You don't need to store the exit code in an explicit variable. You can use 'system' straight in your 'if' condition:
Code:
if ( system("test -f " $4) )
   print $4 " is not a file"
else
   print $4 " is a file"

I hope I could help
# 4  
Old 10-15-2009
Franklin52, sidorenko thank you both for your valuable input, it has help me take further today, still no cigar. Will definitely post when I hit jack pot or getting close and need a little more guidance.
# 5  
Old 10-19-2009
Finally hit the jack pot. again thank you for your contribution which help me move forward along to my destination:

Code:
{
if (sub(/DISCOVERY=ON/, "DISCOVERY=OFF"))
print $0
if ( $1 ~ /^#/ ) {
        print $0 }
if ( NF == 4 ){
    if ( system("test -d " $4) ) {
         }
    else {
        print "1        0       0       "$4 "\n" }
}
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed, awk or another bash command to modify string with the content of another file

Hello everybody, I would like modify some strings using sed or another command line with the content file. For example: - {fqdn: "server-01" , ip: "server-01"} - {fqdn: "server-02" , ip: "server-02"} - {fqdn: "server-03" , ip: "server-03"} - {fqdn: "server-04" , ip: "server-04"} My... (4 Replies)
Discussion started by: dco
4 Replies

2. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

3. Shell Programming and Scripting

Test command non case specific string comparision

Hi, I want to do caseless string comparision using test command for eg: Ind_f="y" test "$Ind_f" == "y|Y" i tried , ** , nothing worked. any thoughts on how to do case insensitive string comparison using test command without converting to any particular case using typeset or tr? (8 Replies)
Discussion started by: Kulasekar
8 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

AWK Command parse a file based on string.

AWK Command parse a file based on string. I am trying to write a shell script to parse a file based on a string and move the content of the file to another file. Here is scenario. File content below Mime-Version: 1.0 Content-Type: multipart/mixed; ... (2 Replies)
Discussion started by: aakishore
2 Replies

7. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

8. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

9. Shell Programming and Scripting

Moving file using test command

My process creates file like abc.20090427.txt i.e abc.date.txt next time when my process it has to detect if any previous "abc" exist. If exist then move to archive and create a new abc file. I am using test command but it doesnt allow wild card. if ] then mv abc.*.txt... (7 Replies)
Discussion started by: pinnacle
7 Replies

10. Shell Programming and Scripting

file descriptor test command

Can someone delve more into what below test command do? if ; then I know -t is for file descriptor -t file descriptor FD (stdout by default) is opened on a terminal But not sure how to relate to it.. $1 is supposedly first argument, so let's say if scriptname:... (2 Replies)
Discussion started by: convenientstore
2 Replies
Login or Register to Ask a Question