A simple find and replace without using any regex (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A simple find and replace without using any regex (bash)
# 1  
Old 03-17-2008
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  
Old 03-18-2008
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.)
era
# 3  
Old 03-18-2008
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

era
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

All combinations from simple regex

Hi ! Before trying to write a code, is there any program or code that generates all the combinations of strings that simple awk regex can match. By "simple regex" I mean let's say without "+", "*", and with a limited number of characters (e.g. from "1" to "5"). e.g: input: 34?5 output:... (9 Replies)
Discussion started by: beca123456
9 Replies

2. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

3. Shell Programming and Scripting

simple Word Capitalization (Title) find/replace

Hi! I'm looking for a simple script, especially a one liner script in tcsh or bash that will emulate the find/replace in all text apps. I want to change all uppercase caracters to Title word (in wich only the first caracter is UpperCase and the rest is lowercase) I can use sed command, but... (2 Replies)
Discussion started by: sstpierre68
2 Replies

4. Shell Programming and Scripting

help with simple regex expression

I am trying to grep the following line in a file using a bash shell: (..) admin1::14959:::::: (..) It works with the following expression (as expected) # cat file | grep ^*:: admin1::14959:::::: but it does not work with (not expected) # cat /etc/shadow | grep ^+:: I assume the... (2 Replies)
Discussion started by: schms
2 Replies

5. Shell Programming and Scripting

Help with simple RegEx on grep

Hello, I am trying to grep my log files for ORA errors, except ORA-00001. I have tried: grep 'ORA*!(-00001)' *.log but it is not working. Any help will be much appreciated. Thank you. (5 Replies)
Discussion started by: drbiloukos
5 Replies

6. Shell Programming and Scripting

Simple find and replace with AWK

I am trying to write a find and replace script with AWK and I can't seem to get it to work. I need it to find this exact string *P*: and replace the P with a T or just replcare the whole thing with *T*:. this is what I have tried awk 'BEGIN {gsub(/\*P*:/,"\*T*:"); print}' ${INFILE} >... (4 Replies)
Discussion started by: wbshrk
4 Replies

7. UNIX for Dummies Questions & Answers

replace line after a regex

Hi, I am trying to write a script which will modify a given account's settings by searching for a line in a file and then replacing the line after it. Here is a portion of my input file: type=friend username=0002 secret=password host=dynamic dtmfmode=rfc2833 mailbox=0002 context=sip... (2 Replies)
Discussion started by: the1armedcoder
2 Replies

8. Shell Programming and Scripting

Simple regex problem?

Hi all, I am looking to create words from a sentence which adhere to a custom search pattern from my website: Example: ! +! / += ~ where the terms ! = not, +! = AND NOT, += - and equals and ~ = can be like.... Now here is the issue...i want to split a sentence like the one above on... (1 Reply)
Discussion started by: muay_tb
1 Replies

9. Shell Programming and Scripting

find, copy and replace text in bash or sh

here is my prob .. i have a very large text files and i need to locate specific lines, copy them and then replace a single word in the replaced text example find all lines that contain '/etc', copy the line immediately below (not at the end of the file) and then replace '/etc' with '/root'... (1 Reply)
Discussion started by: jmvbxx
1 Replies

10. Shell Programming and Scripting

Need Help with Simple Regex

I have got a question. How to do this? I mean AND expression in regex. List all the files in current directory that do not contain the words use AND take. Thx.:p (15 Replies)
Discussion started by: evilfreakz
15 Replies
Login or Register to Ask a Question