Pattern search multiple files

#!/usr/bin/ksh
a="Run successfully"
cd $APPS
ls -l *.txt | while read $txt
do
if [grep -q '$txt'] then 
cp $APPS/$txt cp $hist/$txt
else 
rm $APPS/$txt
echo "Files has been removed"
fi 
done

New in shell script please help me out

Around 100 txt files in $APPS dir i want to search pattern from all the the txt which is generated today or t-1 date files if pattern found then copy else move

There are several syntax errors, as wll as logic errors.

Please tell us what you are trying to do, not how you think it should be done.
Example wild guess:

  1. read a list of filenames
  2. see if each one is in a directory then copy it somewhere
  3. if file is not in the current list - delete it.

Please fix my guess to match what you want. :slight_smile:

Sorry not described clearly
1.Directory contained 10 days .txt files want to search pattern'good script' from all the txt files one by one from current date or t-1 date
2.IF Search patern'good script' from any of the txt files then process---- cp $APP/.txt cp $HIST/.txt (which are contained pattern)
3.or Go to else part if patern'good script' not found all the txt files

#!/usr/bin/ksh
a = "good script "
cd $APPS
ls -l *.txt | while read $txt
do
	if [grep -q 'a' txt] then	
		cp $APP/*.txt cp $HIST/*.txt
         else
		rm $APP/*.txt
		echo "Exit successfully"     
        fi
done

---------- Post updated at 12:18 PM ---------- Previous update was at 12:06 PM ----------

Continue using your script for learning, maybe we can make it work like this:

#!/usr/bin/ksh

pattern="good script"
# Change me to the real directory paths.
APPS="/path/to/apps_directory"
HIST="/path/to/history_directory"

cd $APPS || exit # exist if it cannot cd into $APPS

find . -path './*' -prune -name '*.txt' -type f -mtime -1 | while read filename
do
	if [ grep -q "$pattern" "$filename" ]; then
                # remove # from cp line if successful.
		#cp "$filename" "$HIST/${filename#./}"
                # remove the following line if successful.
                echo "$filename would had been copied to $HIST/${filename#./}"
         else
                # remove rm comment if test is successful.
                # remove the echo line if successful.
		#rm "$filename" && echo "$filename removed"
                echo "$filename would had been deleted"
        fi
done

This was done on the fly. It may contain errors.

Hi Aia,
Thank you for helping Kalia, I think you missed a pair of double-quote characters. With:

pattern="good script"
... ...
	if [ grep -q $pattern "$filename" ]; then

the expansion of $pattern needs to be quoted as well (i.e., "$pattern" ). Without the quotes, grep will be looking for the string good in two files (one file named script and the other file named by the expansion of $filename ) instead of looking for the string good script in a single file.

1 Like
 if [ grep -q $pattern "$filename" ]; 
 if [ grep -q "$pattern" "$filename" ]; 
 

getting error below error

 test.ksh[7]: -q: 0403-012 A test command parameter is not valid.
Exit successfully

Sorry. Our mistake. Try changing:

	if [ grep -q $pattern "$filename" ]; then

to:

	if grep -q "$pattern" "$filename"; then

You don't need or want to use test (AKA [ ... ] ) here; you just want to determine whether or not grep succeeded or not.

1 Like

Output as below.

Exit successfully==== file not contained pattern 
Exit successfully======file not contained pattern 
Exit successfully======file not contained pattern 
Exit successfully======file not contained pattern 

filename would had been copied to $HIST/${filename#./} filed contained.

But i need output should be display only the true part not false part if any of the files contained pattern then copy if not then go to else part

ex:it should be display only this filename would had been copied to $HIST/${filename#./} filed contained.

Please, post your latest iteration of your script.