bash command script to remove line from a file
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > bash command script to remove line from a file




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    bash command script to remove line from a file  
Faouzi Gassemi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-07 06: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





[ Post a follow-up to this message ]



    Re: bash command script to remove line from a file  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Sponsored Links  




 





   All times are GMT. The time now is 02:09 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register