Unix Shell - bash command script to remove line from a file

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2007 > bash command script to remove line from a file





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author bash command script to remove line from a file
Faouzi Gassemi

2007-01-23, 1:23 pm

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.

Thanks in advance
Fanzy
Chris F.A. Johnson

2007-01-23, 1: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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com