1

I am writing a script to read multiple log files and find lines with a specific string. However I am getting an error when trying to egrep/grep on the file.

I have a file that has the names of the log files to read (see below)

files.txt

./code_a.log
./code_b.log

log_analysis.ksh

#!/bin/ksh

platform=$1
lw_platform=$(echo "$platform" | tr '[:upper:]' '[:lower:]')
log_dir="/project/$lw_platform/logs/"
read_file="/user/LogAnalysis/files.txt"
out_file="/user/LogAnalysis/out3.txt"

echo $lw_platform
echo $log_dir

while -r line; do

    echo "Began: $line" >> $out_file

    cd ${log_dir}
    echo $(pwd)

    egrep -n 'ERROR:' $line >> $out_file

    echo "End: $line" >> $out_file

done < $read_file

CONSOLE OUTPUT

test 
/project/test/logs/ 
/project/test/logs 
.grep: 0652-033 Cannot open ./code_a.log

However I don't get this error if I run the same egrep command directly into the shell. Proves that I have the permissions to read the file.

7
  • 1
    Note that you seem to assume that code_a.log and code_b.log are available in the script's current directory. If that is not the case, then either update the text file with the full absolute path of the files, or cd in the scirpt to the correct directory before reading from the file.
    – Kusalananda
    Commented Jul 5, 2021 at 15:46
  • In the script I cd to log_dir which is where these log files are present. Commented Jul 5, 2021 at 16:02
  • 2
    Is the error message prefixed by a single dot, as you have shown? Has the file that you are reading from been edited on a Windows system previous to having been transferred to your AIX system? Does the cd in the script actually succeed? What is the full output of the script when you encounter the error?
    – Kusalananda
    Commented Jul 5, 2021 at 16:44
  • Yes the error message is prefixed by a single dot. The file that I am reading from has been edited in windows before transferring to UNIX server. The cd succeeds (I did pwd to check). Commented Jul 5, 2021 at 18:08
  • The script runs fine if I hard code the filename in egrep command. I get the desired output in the out_file. Commented Jul 5, 2021 at 18:11

1 Answer 1

0

I solved the issue by converting the file from dos line endings to UNIX format. I used the following command.

dos2unix files.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .