bash. Grep text file looking for lines from other file [duplicate] - bash

This question already has answers here:
Intersection of files
(4 answers)
Closed 3 years ago.
Is it possible to analize text file let's say log.txt looking for lines from the other file - let's say config.txt. I tried to do following thing:
while read -r line;
do
if grep -q $line $log_path; then
echo "Found line: $line" >> $out_log
break
else echo "Line not found: $line" >> $out_log
fi
done < $conf_path
$conf_path is a path like /path/to/config.txt
$log_path is a path like /path/to/log.txt
and the $out_log is the /path/to/output_log.txt which is used to test if script works properly.
Example $line is "ERROR: MESSAGE: \[Library not found \!\]". I tested grep instruction outside the script and it found that line properly, so I do not think that grep is the problem here. Every time else statement is executed.
Thank you, Community, for any suggestions,
Best regards, Max

Try below
Code:
while read -r line
do if grep -q "${line}" $log_path
then echo "Found line: $line" >> $out_log
else echo "Line not found: $line" >> $out_log
fi
done < $conf_path
Example:
bash-3.2$ echo $conf_path ; cat $conf_path
config.txt
ERROR: MESSAGE: \[Library not found \!\]
ERROR: MESSAGE: \[File not found \!\]
ERROR: MESSAGE: \[Path not found \!\]
ERROR: MESSAGE: \[Permission denied \!\]
bash-3.2$
bash-3.2$ echo $log_path; cat $log_path
log.txt
INFO: MESSAGE: Script Starts
INFO: MESSAGE: Some command output
ERROR: MESSAGE: [Library not found !]
INFO: MESSAGE: Some other command output
ERROR: MESSAGE: [Path not found !]
INFO: MESSAGE: Exit script with 2
bash-3.2$
bash-3.2$ while read -r line; do if grep -q "${line}" $log_path; then echo "Found line: $line" >> $out_log; else echo "Line not found: $line" >> $out_log; fi; done < $conf_path
bash-3.2$
bash-3.2$ echo $out_log; cat $out_log
output_log.txt
Found line: ERROR: MESSAGE: \[Library not found \!\]
Line not found: ERROR: MESSAGE: \[File not found \!\]
Found line: ERROR: MESSAGE: \[Path not found \!\]
Line not found: ERROR: MESSAGE: \[Permission denied \!\]
bash-3.2$
Hope this helps. Good Luck !!

Related

Any method to grab and use variables from a text file passed as a parameter in a bash script?

I have a txt file named setup.txt that contains the following data:
DSN=ZXP.SOURCE
PATH=/z/zxp/20211015
SCHEMA=ZXP
URL=https://zxp-support.mybluemix.net/contest/4q21/
OUTPUT=OUTPUT(RESULT)
REPORT=q421report
I am able to grab the variables some help to use declare but the I am not able to execute commands down below in the script
input="$1"
if [ $# -ne 1 ]
then
printf "This program takes the input from a file (first argument)\n"
exit 1
fi
if test -f "$input"; then
echo "$input exists."
else
echo "File $input does not exist, or is not a file"
exit 1
fi
mapfile -t a < $1
declare "${a[#]}"
db="$DSN"
path="$PATH"
schema="$SCHEMA"
url="$URL"
output="$OUTPUT"
report="$REPORT"
DB2="java com.ibm.db2.clp.db2"
$DB2 -tvf decrypt.sql > parse_decryption1.txt
key=$(python3 decryption.py)
sed -e 's/schema/'"${schema}"'/g;s/pass/'"${key}"'/g' drop3.sql > finaldrop4.sql
$DB2 -tvf finaldrop4.sql > raw_data.txt
python3 drop4.py ${url} > drop5output.txt
I get the following erros:
./q421drop5.sh: line 30: java: command not found
./q421drop5.sh: line 31: python3: command not found
./q421drop5.sh: line 32: sed: command not found
./q421drop5.sh: line 33: java: command not found
./q421drop5.sh: line 36: python3: command not found
./q421drop5.sh: line 37: cp: command not found
How can I access the variables and still be able to execute other commands below?

Why do I get a "no such file or directory" error in this Bash script?

I have this small part of bash script that gets output and writes/echos the output, but it states
./test.sh: line 24: : No such file or directory
while IFS= read -r line; do
cat << EOF
- request:
path: $line
method: GET
response:
raw_body: $res
status: $code
EOF
done < "$1"
Why is that?
Instead of launching sh test.sh, you should launch sh test.sh <input_file>, like this, your input parameter $1 will be filled in.

bash file gives error "syntax error near unexpected token `fi'"

When trying to execute my script below, I get this error:
syntax error near unexpected token `fi'
file:Ccamcheck.sh is in usr/script
My script:
#!/bin/sh
if ps x |grep -v grep |grep -c CCcam >/dev/null
then
echo "cccam... ok"
echo && date >> /var/log/CCcam.ok
else
echo "cccam... restarting"
echo && date >> /var/log/CCcam.check
CCcam
fi
Does anyone know why I am receiving this error?

Writing variable's content to a variable file inside a variable-directory

Let's say I write the following script:
#!/bin/sh
TEXT="hello"
DIR="test"
FILE="bla.txt"
echo $TEXT > $DIR/$FILE
When running it, I get the following error:
test.sh: line 5: test/bla.txt: No such file or directory
But if change the last line to:
echo $TEXT > $FILE
I don't get the error.
I know I can work around it and do:
FILE_TO_WRITE_TO=$DIR/$FILE
echo $TEXT > $FILE_TO_WRITE_TO
But I don't understand why does the error occur in the first place.
The following command gives No such file or directory error because test directory doesn't exist or Not a directory if it is not a directory.
echo "hello" > test/bla.txt
To create the directory if necessary, and error management.
mkdir -p "$DIR" || { echo "failed to mkdir $DIR"; exit 1;}
echo "$TEXT" > "$DIR/$FILE" || { echo "failed to open $DIR/$FILE for writting"; exit 1;}

error on last done preventing further processing in bash

The below bash seems to error on the last done. What is the correct syntax as I can not seem to figure it out without created more errors. There are some additional processes after that will not run because an error is thrown currently. Thank you :).
error
/home/cmccabe/Desktop/loop.sh: line 79: syntax error near unexpected token `done'
/home/cmccabe/Desktop/loop.sh: line 79: `done >> "$logfile"'
bash
logfile=/home/cmccabe/Desktop/NGS/API/6-2-2016/process.log
for file in /home/cmccabe/Desktop/NGS/API/6-2-2016/vcf/overall/stats/*.vcf ; do
echo "Start annovar creation: $(date) - file: $file"
echo ${file##*/} >> /home/cmccabe/Desktop/NGS/annovar/target.txt
cp /home/cmccabe/Desktop/NGS/API/6-2-2016/vcf/overall/stats/*.vcf /home/cmccabe/Desktop/NGS/annovar
echo "End annovar file creation: $(date) - file: $file"
done
logfile=/home/cmccabe/Desktop/NGS/API/6-2-2016/process.log
cd "/home/cmccabe/Desktop/NGS/annovar"
$( perl -ne 'chomp; system ("perl table_annovar.pl -vcfinput $_ humandb/ -buildver hg19 -arg '-hgvs',,,,,,,,,, -remove -protocol IDP.refGene,avsnp147,popfreq_all_20150413,spidex,ljb26_sift,ljb26_pp2hdiv,ljb26_pp2hvar,ljb26_lrt,ljb26_mt,ljb26_ma,clinvar_20160302 -operation g,f,f,f,f,f,f,f,f,f,f")' < target.txt )
mv /home/cmccabe/Desktop/NGS/annovar/*multianno.txt /home/cmccabe/Desktop/NGS/API/6-2-2016/vcf/overall/annovar
echo "End annovar annotation creation: $(date) - file: $file"
done >> "$logfile"
You want the >> at the echo line, as such. The done is also redundant; there's no loop to close:
logfile=/home/cmccabe/Desktop/NGS/API/6-2-2016/process.log
cd "/home/cmccabe/Desktop/NGS/annovar"
$( perl -ne 'chomp; system ("perl table_annovar.pl -vcfinput $_ humandb/ -buildver hg19 -arg '-hgvs',,,,,,,,,, -remove -protocol IDP.refGene,avsnp147,popfreq_all_20150413,spidex,ljb26_sift,ljb26_pp2hdiv,ljb26_pp2hvar,ljb26_lrt,ljb26_mt,ljb26_ma,clinvar_20160302 -operation g,f,f,f,f,f,f,f,f,f,f")' < target.txt )
mv /home/cmccabe/Desktop/NGS/annovar/*multianno.txt /home/cmccabe/Desktop/NGS/API/6-2-2016/vcf/overall/annovar
# See here
echo "End annovar annotation creation: $(date) - file: $file" >> "$logfile"

Resources