Sponsored Content
Top Forums Shell Programming and Scripting Script for searching a pattern in 5 files and deleting the patterns given Post 302337525 by Shazin on Friday 24th of July 2009 10:13:55 AM
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!
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
EXIGREP(8)						      System Manager's Manual							EXIGREP(8)

NAME
EXIGREP - Search Exim's main log SYNOPSIS
exigrep [-l] pattern [log file] ... DESCRIPTION
The exigrep utility is a Perl script that searches one or more main log files for entries that match a given pattern. When it finds a match, it extracts all the log entries for the relevant message, not just those that match the pattern. Thus, exigrep can extract complete log entries for a given message, or all mail for a given user, or for a given host, for example. The -l flag means 'literal', that is, treat all characters in the pattern as standing for themselves. Otherwise the pattern must be a Perl regular expression. The pattern match is case-insensitive. If no file names are given on the command line, the standard input is read. If the location of a zcat command is known from the definition of ZCAT_COMMAND in Local/Makefile, exigrep automatically passes any file whose name ends in COMPRESS_SUFFIX through zcat as it searches it. BUGS
This manual page needs a major re-work. If somebody knows better groff than us and has more experience in writing manual pages, any patches would be greatly appreciated. SEE ALSO
exim(8), perlre(1), /usr/share/doc/exim4-base/ AUTHOR
This manual page was stitched together from spec.txt by Andreas Metzler <ametzler at downhill.at.eu.org>, for the Debian GNU/Linux system (but may be used by others). March 26, 2003 EXIGREP(8)
All times are GMT -4. The time now is 08:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy