![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 01:20 PM |
| regex test in bash | subin_bala | Shell Programming and Scripting | 1 | 04-16-2008 04:27 AM |
| Simple BASH script? | JayC89 | Shell Programming and Scripting | 16 | 10-02-2007 08: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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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! |
|
||||
|
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.) |
![]() |
| Bookmarks |
| Tags |
| regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|