FotisK
FotisK

Reputation: 1177

how to check if file exist and not empty in awk using wildcard in filename or path

I am trying to create a small awk line that should go through several paths and in each path find a specific file that should not be empty (wildcard). If the file is not found or empty it should print "NULL". I did some searching in stackoverflow and other places but couldn't really make it work.

Example: path is /home/test[1..5]/test.json

awk -F"[{}]" '{ if (system("[ ! -f FILENAME ]") == 0 && NR > 0 && NF > 0) print $2; else print "NULL"}' /home/test*/test.txt

If the test.txt is empty or does not exists it should print "NULL" but meanwhile when it is not empty it should print $2.

In the above example it will just skip the empty file and not write "NULL"!

Example execution /home/ has test1, test2, test3 path and each path has one test.txt (/home/test1/test.txt is empty):

The test.txt file in each of the /home/test* path will be empty or the below kind of text (always one line): {"test":1033}

# awk -F"[{}]" '{ if (system("[ ! -f FILENAME ]") == 0 && NR > 0 && NF > 0) print $2; else print "NULL"}' /home/test*/test.txt
"test":1033
"test":209

File examples:

/home/test0/test.txt (not empty -> {"test":1033})
/home/test1/test.txt (empty)
/home/test2/test.txt (not empty -> {"test":209})
/home/test3/test.txt (not exist)

But for ../test1/test.txt I would like to see "NULL" but instead I see nothing!

I would like to have a printout like the below:

"test":1033
NULL
"test":209
NULL

What am I doing wrong?

BR

Upvotes: 1

Views: 1593

Answers (2)

Ed Morton
Ed Morton

Reputation: 203324

for dir in home/test*; do
    file="$dir/test.txt"
    if [ -s "$file" ]; then
        # exists and is non-empty
        val=$( awk -F'[{}]' '{print $2}' "$file" )

    else
        # does not exist or is empty
        val="NULL"
    fi
    printf '%s\n' "$val"
done

Upvotes: 0

John1024
John1024

Reputation: 113834

If I understand what you are asking correctly, there is no need for a system call. One can use ENDFILE to check to see if a file was empty.

Try this:

awk -F"[{}]" '{print $2} ENDFILE{if(FNR==0)print "NULL"}' /home/test*/test.txt

FNR is the number of records in a file. If FNR is zero at the end of a file, then that file had not records and we print NULL.

Note: Since this solution use ENDFILE, Ed Morton points out that GNU awk (sometimes called gawk) is required.

Example

Suppose that we have these three files:

$ ls -1 home/test*/test.txt
home/test1/test.txt
home/test2/test.txt
home/test3/test.txt

All are empty except home/test2/test.txt which contains:

$ cat home/test2/test.txt
first{second}
1st{2nd}

Our command produces the output:

$ awk -F"[{}]" '{print $2} ENDFILE{if(FNR==0)print "NULL"}' home/test*/test.txt
NULL
second
2nd
NULL

Test for non-existent files

for d in home/test*/; do [ -f "$d/test.txt" ] || echo "Missing $d/test.txt"; done

Sample output:

$ for d in home/test*/; do [ -f "$d/test.txt" ] || echo "Missing $d/test.txt"; done
Missing home/test4//test.txt

Upvotes: 1

Related Questions