Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Pattern search multiple files

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
Kalia Kalia is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 19 June 2017, 8:51 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Pattern search multiple files


Code:
#!/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
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 27 June 2017, 8:38 AM EDT
Location: NM
Posts: 11,095
Thanks: 537
Thanked 1,068 Times in 990 Posts
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. Linux
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
Kalia Kalia is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 19 June 2017, 8:51 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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


Code:
#!/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


Last edited by Scott; 1 Week Ago at 12:45 PM.. Reason: Please use code tags
    #4  
Old Unix and Linux 1 Week Ago
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 June 2017, 6:17 PM EDT
Posts: 1,633
Thanks: 46
Thanked 622 Times in 583 Posts
Quote:
Originally Posted by Kalia View Post
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


Code:
#!/usr/bin/ksh
a = "good script " # There cannot be spaces between variable name and assignment 
cd $APPS # APPS needs to have an assignment before use or it will be empty
ls -l *.txt | while read $txt # $ is to deference a variable (the value in a variable). Remove the $. Also remove the -l out of ls, that would output extra that it will interfere
do
	if [grep -q 'a' txt] then # txt is the variable name, you want the value in the variable. It needs $txt
		cp $APP/*.txt cp $HIST/*.txt # $APP is not the same than $APPS. In any case $APP and $HIST have not been declared before in the script
                 # * is a glob to indicate anything. *.txt will mean file1.txt file2.txt, file3.txt. You do not want that. You want just $txt
                 # There must be spaces between [ grep ... ]
                 # There must be a semicolon before the then
         else
		rm $APP/*.txt # apply the same comments from above
		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:


Code:
#!/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.

Last edited by Aia; 1 Week Ago at 09:22 PM.. Reason: Add missing pair double-quote in the dereferencing of $pattern.
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 26 June 2017, 8:37 PM EDT
Location: San Jose, CA, USA
Posts: 10,396
Thanks: 527
Thanked 3,627 Times in 3,093 Posts
Hi Aia,
Thank you for helping Kalia, I think you missed a pair of double-quote characters. With:

Code:
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.
The Following User Says Thank You to Don Cragun For This Useful Post:
Aia (1 Week Ago)
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago
Kalia Kalia is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 19 June 2017, 8:51 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
 if [ grep -q $pattern "$filename" ]; 
 if [ grep -q "$pattern" "$filename" ];

getting error below error

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


Moderator's Comments:
Pattern search multiple files Please use CODE tags - for data as well - as required by forum rules!

Last edited by RudiC; 1 Week Ago at 10:12 AM.. Reason: Added CODE tags.
Sponsored Links
    #7  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 26 June 2017, 8:37 PM EDT
Location: San Jose, CA, USA
Posts: 10,396
Thanks: 527
Thanked 3,627 Times in 3,093 Posts
Quote:
Originally Posted by Kalia View Post
Code:
 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:

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

to:

Code:
	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.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search multiple pattern in a file saurau Shell Programming and Scripting 4 08-04-2015 09:08 AM
awk with multiple pattern search Manasa Pradeep Shell Programming and Scripting 2 06-25-2013 02:36 AM
Multiple search pattern Sara_84 Shell Programming and Scripting 4 04-02-2013 09:04 AM
pattern search for multiple log files and counting gubbu Shell Programming and Scripting 3 09-10-2009 03:57 PM
Pattern search in multiple lines shanky09 Shell Programming and Scripting 2 04-02-2009 04:29 AM



All times are GMT -4. The time now is 04:56 PM.