01-23-07 06:23 PM
On 2007-01-23, Faouzi Gassemi wrote:
> Hi all
>
> i would like to make a grep of a pattern and remove the line from a file.
>
> example :
>
> user1:whhwhwh:/home/user1:/bin/bash
> user2:awhhwhwh:/home/user1:/bin/bash
> user3:wxxhhwhwh:/home/user1:/bin/bash
> user4:whhccwhwh:/home/user1:/bin/bash
> user5:whhwddhwh:/home/user1:/bin/bash
>
> i would like to have a bash script that have as entry like :
>
> script user1
>
> this is the line for user1 " user1:whhwhwh:/home/user1:/bin/bash"
>
> Would you like to remove user1 one?(y/n)y
>
> then remove user1 line from the file.
line=$( grep "$1" FILENAME )
if [ -n "$line" ]
then
## This read command is bash specific; adjust for other shells
read -ep "Would you like to remove $1 (y/n)? " -sn1
case $REPLY in
y|Y) grep -v "$1" FILENAME > tempfile ;; ## Do whatever with tempfile
esac
else
printf "%s\n" "$1 not found in FILENAME" >&2
fi
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
[ Post a follow-up to this message ]
|