Pattern search multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern search multiple files
# 1  
Old 06-16-2017
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
# 2  
Old 06-16-2017
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. Smilie
# 3  
Old 06-18-2017
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; 06-18-2017 at 01:45 PM.. Reason: Please use code tags
# 4  
Old 06-18-2017
Quote:
Originally Posted by Kalia
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; 06-18-2017 at 10:22 PM.. Reason: Add missing pair double-quote in the dereferencing of $pattern.
# 5  
Old 06-18-2017
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.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 06-19-2017
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:
Mod Comment Please use CODE tags - for data as well - as required by forum rules!

Last edited by RudiC; 06-19-2017 at 11:12 AM.. Reason: Added CODE tags.
# 7  
Old 06-19-2017
Quote:
Originally Posted by Kalia
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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search multiple pattern in a file

I have a sample file with following output: HTTP/1.1 200 OK User: admin Set-Cookie: AMBARISESSIONID=y3v3648yqcno32nq478kw7ar;Path=/;HttpOnly Expires: Thu, 01 Jan 1970 00:00:00 GMT Content-Type: text/plain Vary: Accept-Encoding, User-Agent Content-Length: 6057 Server:... (4 Replies)
Discussion started by: saurau
4 Replies

2. Linux

Search multiple pattern from list

I am working on AIX operating system. I want to search list of Article Id for given Set Date (which are present in a seperate file input.txt) art_list.csv ------------ "Article ID" |"Ad Description" |"Pyramid"|"Pyramid Desc "|"ProductTypeId"|"Set Date "|... (3 Replies)
Discussion started by: rajivrsk
3 Replies

3. Shell Programming and Scripting

awk with multiple pattern search

Want to fetch a column with multiple pattern using awk. How to achieve the same. Tried cat test address : 10.63.20.92/24 address : 10.64.22.93/24 address : 10.53.40.91/24 cat test | awk '{print $3}' |awk -F "/" '{print $1}' 10.63.20.92 10.64.22.93 10.53.40.91 Is there any... (2 Replies)
Discussion started by: Manasa Pradeep
2 Replies

4. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

5. Shell Programming and Scripting

Multiple search pattern

Hello :) I have this file cat employee_list Name : jack Gender: m ID : 4512 DOB : 03/27/1980 hire date : 04/23/2012 Nationality: US marital status : single ===================== Name : mick Gender: m ID : 1256 DOB : 03/27/1970 Hire date : 012/10/2011 Nationality: US Marital... (4 Replies)
Discussion started by: Sara_84
4 Replies

6. Shell Programming and Scripting

Help need with PERL multiple search pattern matching!

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (3 Replies)
Discussion started by: sags007_99
3 Replies

7. Shell Programming and Scripting

Multiple pattern search in perl

user 10 values content is: musage.py yes value user 11 values content is: gusage.py yes value how to print "user" string line by searching "content is:" string and "usage.py" string in perl (8 Replies)
Discussion started by: Anjan1
8 Replies

8. Shell Programming and Scripting

pattern search for multiple log files and counting

I have 10 appservers and each appserver has 4 jvms . Each of these logs is archived and stored on a nfs directory . For example the files are /logs/200907/ap1-jvm1.server.log.20090715.gz /logs/200907/ap2-jvm2.server.log.20090714.gz /logs/200908/ap1-jvm1.server.log.20090812.gz I want to... (3 Replies)
Discussion started by: gubbu
3 Replies

9. Shell Programming and Scripting

Pattern search in multiple lines

Hi, I have to search those statements from the file which starts from "shanky"(only shanky, shanky09 or 09shanky is not allowed) and ends with ");". These two string can be in a same line or different line. And also i have to negate those lines which starts with #. Can any one please give me... (2 Replies)
Discussion started by: shanky09
2 Replies
Login or Register to Ask a Question