Script for searching a pattern in 5 files and deleting the patterns given


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for searching a pattern in 5 files and deleting the patterns given
# 1  
Old 07-24-2009
Script for searching a pattern in 5 files and deleting the patterns given

Hi All,

I have written the below script that searches for the pattern in a file and delete them if present. please can some one have a look and suggest the changes in the script.

Code:
#!bin/sh

# The pattern that user want to add to the files

echo "Enter the pattern of the redirect"
read value

# check if the user has provided a valid pattern

if [-z "$value"] ; then
echo "You have not entered the pattern" ; exit 1

# If a pattern is entered by the User
# Searches the pattern and deletes the pattern if present in file (file 1) <filename 1>

mkdir /path/dred1
elif [grep "$value" <filename 1> > /path/dred1 && $? == 0] ; then
rm -r /path/dred1
echo "Pattern exist in <filename 1>"
sleep 1000
mkdir /path/olist
sed '/"$value"/d' <filename 1> > /path/olist
/path/olist > <filename 1>
rm -r /path/olist
echo "Pattern deleted"
else
rm -r /path/dred1
echo "Pattern does not exist in <filename 1>"
fi

# Pattern to be searched in (file 2) <filename 2> and delete it if present

mkdir /path/dred2
elif [grep "$value" <filename 2> > /path/dred2 && $? == 0] ; then
rm -r /path/dred2
echo "Pattern exist in <filename 2>"
sleep 1000
mkdir /path/olist
sed '/"$value"/d' <filename 2> > /path/olist
/path/olist > <filename 2>
rm -r /path/olist
echo "Pattern deleted"
else
rm -r /path/dred2
echo "Pattern does not exist in <filename 2>"
fi

# Pattern to be searched in (file 3) <filename 3> and delete it if present

mkdir /path/dred3
elif [ grep "$value" <filename 3> > /path/dred3 && $? == 0] ; then
rm -r /path/dred3
echo "Pattern exist in <filename 3>"
sleep 1000
mkdir /path/olist
sed '/"$value"/d' <filename 3> > /path/olist
/path/olist > <filename 3>
rm -r /path/olist
echo "Pattern deleted"
else
rm -r /path/dred3
echo "Pattern does not exist in <filename 3>"
fi

# Pattern to be searched in <filename 4> <filename 4> and delete it if present

mkdir /path/dred4
elif [grep "$value" <filename 4> > /path/dred4 && $? == 0] ; then
rm -r /path/dred4
echo "Pattern exist in <filename 4>"
sleep 1000
mkdir /path/olist
sed '/"$value"/d' <filename 4> > /path/olist
/path/olist > <filename 4>
rm -r /path/olist
echo "Pattern deleted"
else
rm -r /path/dred4
echo "Pattern does not exist in <filename 4>"
fi

# Pattern to be searched in <filename 5> <filename 5> and delete it if present

mkdir /path/dred5
elif [grep "$value" <filename 5> > /path/dred5 && $? == 0] ; then
rm -r /path/dred5
echo "Pattern exist in <filename 5>"
sleep 1000
mkdir /path/olist
sed '/"$value"/d' <filename 5> > /path/olist
/path/olist > <filename 5>
rm -r /path/olist
echo "Pattern deleted"
else
rm -r /path/dred5
echo "Pattern does not exist in <filename 5>"
fi

# Close loop for outer loop
fi

Next time use CODE-tags when posting code, data or logs to enhance readability and to preserve formatting like indention etc., ty.

Thanks and Regards,
Shazin

Last edited by zaxxon; 07-24-2009 at 11:17 AM.. Reason: I am a CODE-tag junky!
# 2  
Old 07-24-2009
It should be easier if you post small samples of your input files and and explain what exactly you're trying to achieve.
# 3  
Old 07-24-2009
Hi,

I am just trying to search for a word in a file and delete that complete line from the file if that word is present.

Cheers,
Shazin

---------- Post updated at 08:11 PM ---------- Previous update was at 08:06 PM ----------

Hi,

It can also be like this if we take it for a single file:

#!bin/sh

# The pattern that user want to add to the files

echo "Enter the pattern of the redirect"
read value

# check if the user has provided a valid pattern

if [-z "$value"] ; then
echo "You have not entered the pattern" ; exit 1

# If a pattern is entered by the User
# Searches the pattern and deletes the pattern if present in file (file 1) <filename 1>

mkdir /path/dred1
elif [grep "$value" <filename 1> > /path/dred1 && $? == 0] ; then
rm -r /path/dred1
echo "Pattern exist in <filename 1>"
sleep 1000
mkdir /path/olist
sed '/"$value"/d' <filename 1> > /path/olist
/path/olist > <filename 1>
rm -r /path/olist
echo "Pattern deleted"
else
rm -r /path/dred1
echo "Pattern does not exist in <filename 1>"
fi
fi

Please advise
Shazin
# 4  
Old 07-24-2009
This seems to be a duplicate post of:

https://www.unix.com/shell-programmin...g-pattern.html
# 5  
Old 07-25-2009
Yes,
it is. Continue here.
Thread closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Searching a pattern in .gz files

in a directory, I have some files(comma seperated) and some .gz files (each .gz file contain one file which again is comma seperated). I want to search and write the names of all those files which contain any particular value (say 1150) at any specified field position(say 10th field). How di I do... (1 Reply)
Discussion started by: Kumar Jivi
1 Replies

2. Shell Programming and Scripting

Awk script searching patterns issue

Hi I am having a file like this FILE1 ##################### C16ROTINV_ REFCLK_RXL RXBCLK32_R REFCLK_TXL CLK8_TXLIN RXBCLK32_R DCLK_TXLIN CLK32D_TXL RXACLK32_R ##################### (3 Replies)
Discussion started by: jaita
3 Replies

3. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

4. Shell Programming and Scripting

Searching for multiple patterns in files

I have a situation where I need to search for multiple strings (error messages) such as 'aborted' 'file not found' etc in directory having logs. I have put all the error messages in a text file and using the command. grep -f <textfile> <filetobegrepped> I'm doing this thru a script where I... (5 Replies)
Discussion started by: bornon2303
5 Replies

5. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

6. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

7. Shell Programming and Scripting

Searching all files that contain pattern

Hello All, i have to search a pattern in all the files in all subfolders that are present in current directory. suppose i am in d1 directory and in that sd1,sd2,sd3 are subdirectories. in sd1 i have files f1,f2 sd2 i have files f3,f4 sd3 i have file f5 i have to list out all those... (4 Replies)
Discussion started by: ravi.sadani19
4 Replies

8. Shell Programming and Scripting

Delete lines between two patterns without deleting the second pattern

I want to delete lines like this sed '/FROM_HERE/,/TO_HERE/d' but I would like to *not* delete the second match, i.e. the TO_HERE line. How can I achieve this? Thank you! (1 Reply)
Discussion started by: Ilja
1 Replies

9. UNIX for Dummies Questions & Answers

Searching for files with certain string pattern

Hello All I would like to search for files containing certain string pattern under all the directories under /vobs/vobname and print the output to a file in my home directory. How can I do this? Note: /vobs/vobname conatins several directories. Thank You in advance newbetounix (1 Reply)
Discussion started by: intrigue
1 Replies

10. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies
Login or Register to Ask a Question