May
May

Reputation: 1167

awk: fatal: cannot open file ' for reading (No such file or directory)

I have a directory which contains a lot of files with .sum extension. I used the below script to list the contents of all the .sum files to a temp.log file. While the first .sum file gets written to temp.log the awk utility seems to give error for the remaining .sum files. Please help, what i am missing in here.

cd $HOME/aphp/result/${test}



for filename in *.sum
do
tempdir=$filename
awk '/Failed/' "${filename}" > temp.log
awk '/Error/'  "${filename}" >> temp.log
    if [ -s temp.log ]
    then        

    mkdir -p ${scanresult}/${tempdir}
    mv temp.log ${scanresult}/${tempdir}/temp.log
    cd ${scanresult}/${tempdir}
    mv temp.log ${tempdir}_failed.txt
    else
    echo Skipping ${tempdir} scanning as it is executed 100 percent with no fail or error.
    rm temp.log      
    fi
done

Errors:

awk: fatal: cannot open file `dss154.sum' for reading (No such file or directory)
awk: fatal: cannot open file `dss235.sum' for reading (No such file or directory)
awk: fatal: cannot open file `dss287.sum' for reading (No such file or directory)

ls -l *.sum

-rwxrwxrwx 1 smruti smruti 1844 Mar 25 16:23 dss103.sum
-rwxrwxrwx 1 smruti smruti 2353 Mar 25 16:40 dss154.sum
-rwxrwxrwx 1 smruti smruti 1023 Mar 25 16:43 dss235.sum
-rwxrwxrwx 1 smruti smruti  908 Mar 25 16:45 dss287.sum
-rwxrwxrwx 1 smruti smruti  867 Mar 25 16:45 dss288.sum
-rwxrwxrwx 1 smruti smruti 1064 Mar 25 16:47 dss350.sum

Upvotes: 1

Views: 12683

Answers (1)

anubhava
anubhava

Reputation: 785531

You are getting into this problem due to this line:

cd ${scanresult}/${tempdir}

Which is changing your current working directory to something else. After which rest of the files cannot be read after 1st file. It is not really clear why are you changing directory inside the loop.

You can use this line to go back to original path:

cd -

However most of your code after awk command looks suspicious and redundant.

Upvotes: 3

Related Questions