search a word and copy the file

Hi need help with a script or command

My requirement is

  • I need to do a "ls -ltr tcserver*.syslog" files in /tmp, direct the output to a file
ls -ltr tcserv*.syslog | grep "Jan 31" | awk '{printf "\n" $9}' > jandat.logs

-Then open each file in above list and search for string/word, if the word is there then copy the file name to another out put file

cat jandat.logs| while read LOG;do
    grep -i "EXCEPTION: O/S ERROR: signal  6" $LOG  && echo ${LOG} >> jansig.log

some how above script is not working, here is the full script

#!/bin/ksh -x
DATE=$1
ls -ltr tcserv*.syslog | grep "Jan 31" | awk '{printf "\n" $9}' > jandat.logs
cat jandat.logs| while read LOG;do
    grep -i "EXCEPTION: O/S ERROR: signal  6" $LOG  && echo ${LOG} >> jansig.log
done

Please, can we have sample of jandat.logs, jansig.log. Also a script is suppose to work, even if that contains error or gives undesired output. So by not working, I assume, you are not getting desired output :slight_smile:

Also, please try this:

#!/bin/ksh -x
DATE=$1
ls -ltr tcserv*.syslog | awk '\Jan 31\ {printf "\n" $9}' > jandat.logs
while read LOG;do
    STR=$(grep -i "EXCEPTION: O/S ERROR: signal  6" $LOG)  
    [[ -z ${STR} ]] || echo ${LOG} >> jansig.log
    unset STR
done < jandat.logs
 

Here is smaple of ls -lrt tcserver*.syslog :

-rw-r--r--   1 infodba  tcgrp       6935 Feb  1 15:00 tcserver16557.syslog
-rw-r--r--   1 infodba  tcgrp       6935 Feb  1 15:03 tcserver16675.syslog
 

so my first line of script should copy names of tcserver*.syslog created on Feb 1 to a file1, then i need to read all the files to see if they have "EXCEPTION: O/S ERROR: signal 6" and then copy those name to File2

---------- Post updated at 05:46 AM ---------- Previous update was at 05:25 AM ----------

here is what i get wehn execute the script, (it create the list of file in jandat.logs) but while reading it failes

+ [[ -z  ]]
+ unset STR
+ read LOG
+ + grep -i EXCEPTION: O/S ERROR: signal  6 -rw-r--r-- 1 infodba tcgrp 263630 Dec 21 17:38 tcserver20953.syslog
grep: can't open -rw-r--r--
grep: can't open 1
grep: can't open tcgrp
grep: can't open 263630
grep: can't open Dec
grep: can't open 21
grep: can't open 17:38
STR=
+ [[ -z  ]]
+ unset STR
+ read LOG
+ + grep -i EXCEPTION: O/S ERROR: signal  6 -rw-r--r-- 1 infodba tcgrp 263630 Dec 21 17:44 tcserver21009.syslog
grep: can't open -rw-r--r--
grep: can't open 1
grep: can't open tcgrp
grep: can't open 263630
grep: can't open Dec
grep: can't open 21
grep: can't open 17:44
STR=
+ [[ -z  ]]
+ unset STR
+ read LOG
+ + grep -i EXCEPTION: O/S ERROR: signal  6 -rw-r--r-- 1 infodba tcgrp 263630 Dec 21 18:02 tcserver21188.syslog
grep: can't open -rw-r--r--
grep: can't open 1
grep: can't open tcgrp
grep: can't open 263630
grep: can't open Dec
grep: can't open 21
grep: can't open 18:02
STR=
+ [[ -z  ]]
+ unset STR
+ read LOG

Yiikkkees... :wall: My Bad...

Here you go:

 
#!/bin/ksh -x
DATE=$1
ls -ltr tcserv*.syslog | awk '/Jan 31/ {print $9}' > jandat.logs
while read LOG;do
    STR=$(grep -i "EXCEPTION: O/S ERROR: signal  6" $LOG 2>/dev/null)
    [[ -z ${STR} ]] || echo ${LOG} >> jansig.log
    unset STR
done < jandat.logs 
 
ls -lrt tcserver*.syslog | awk '/Feb 1/ {print $NF}' | while read file; do grep -l "EXCEPTION: O\/S ERROR: signal 6" $file && echo "$file has the pattern" >>output.txt ; done