The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
dont't find right regex trek Shell Programming and Scripting 3 05-27-2008 12:20 PM
regex test in bash subin_bala Shell Programming and Scripting 1 04-16-2008 03:27 AM
Simple BASH script? JayC89 Shell Programming and Scripting 16 10-02-2007 07:23 PM
find -regex: matching multiple extensions r0sc0 Shell Programming and Scripting 2 12-08-2005 02:32 PM
command find returned bash: /usr/bin/find: Argument list too long yacsil Shell Programming and Scripting 1 12-15-2003 06:38 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-17-2008
srikanths srikanths is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 4
A simple find and replace without using any regex (bash)

Hi,

I need to do an exact find and replace (I don't want to use regular expressions because the input comes from user). I want to find a line that matches the user's input text and replace it with an empty string.

For example, let's say the user enters I love "Unix" and the contents of the file where I want to do find and replace is this:

Code:
I like "Unix" more than DOS
I love "Unix"
I said I love "Unix"
I love "Unix" a lot
Now I want to replace the line I love "Unix" with empty string. And I want to leave the remaining lines like I said I love "Unix" and I love "Unix" a lot as is. The double-quotes are intentional.

Which Unix utility can do this? An example would be great! I searched and found that sed is something close what I am looking for, but it takes a regular expression. Because the input string is coming from the user, it's tough for me to generate a regular expression for that. Any other ways?

Thanks!
  #2 (permalink)  
Old 03-18-2008
era
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
man grep

Code:
read input
fgrep -vx "$input" file
It removes the line completely, and doesn't even leave the newline. If you require the newline to be preserved, it's a bit harder.

It's not too tough to generate a proper regular expression, per se, but it tends to be a bit ugly.

Code:
read input
echo "$input" | sed -e 's/[][\\.*^$]/\\&/g; s/.*/s%^&\$%%/' | sed -f - file
This is untested, and intended merely as a proof of concept. I probably forgot one or two special characters which need to be backslash-escaped. (Hint: remove the final part of the pipeline to see the generated script.)
  #3 (permalink)  
Old 03-18-2008
era
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Oh, and if you are really strict about bash, try this:

Code:
read input
while read line; do
  case $line in
    $input) echo ;;  # or not, if you want the newline gone, too
    *) echo "$line";;
  esac
done <file
Sponsored Links
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0