Search String and Replace It


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search String and Replace It
# 1  
Old 12-16-2011
Search String and Replace It

Hello,

I'm creating a small script where the user will enter a string that is currently in the file, than enter another string that will replace the first string. I'm a little unsure to go about doing this, here is what I've tried so far with no luck.

Code:
addressfile=~/address_file
clear
tput cup 5 1
echo "Enter Phone Number To Edit: "
tput cup 5 35
read number
clear
tput cup 5 2
echo "Enter Replacement Number: "
tput cup 5 37
read numedit
grep $number $addressfile
sed -i 's/$number/$numedit/g' $addressfile
echo "Press ENTER to continue..."
read continue

Any help is greatly appreciate!
# 2  
Old 12-16-2011
Change the code like this:

Code:
addressfile=~/address_file
clear
tput cup 5 1
echo "Enter Phone Number To Edit: "
tput cup 5 35
read number
clear
tput cup 5 2
echo "Enter Replacement Number: "
tput cup 5 37
read numedit
grep "$number" "$addressfile"
sed -i "s/$number/$numedit/g" "$addressfile"
echo "Press ENTER to continue..."
read continue

# 3  
Old 12-16-2011
Quote:
Originally Posted by radoulov
Change the code like this:

Code:
addressfile=~/address_file
clear
tput cup 5 1
echo "Enter Phone Number To Edit: "
tput cup 5 35
read number
clear
tput cup 5 2
echo "Enter Replacement Number: "
tput cup 5 37
read numedit
grep "$number" "$addressfile"
sed -i "s/$number/$numedit/g" "$addressfile"
echo "Press ENTER to continue..."
read continue

Thanks a ton man! Was going on with this for about and hour, forgot about the quotes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

3. Shell Programming and Scripting

Search and Replace string

Need a script to traverse all files and folders under "/appli/logs" and replace the string "Hello" with "Yellow" whereever found. x86_64 x86_64 x86_64 GNU/Linux (4 Replies)
Discussion started by: mohtashims
4 Replies

4. Shell Programming and Scripting

Search a string and replace the same string above two lines?.

I need to search this "<CardinalMPI>" string and replace the same string above two lines of the string. Nov 16, 2012 12:58:34 PM INFO: Centinel LookUp ResponseXML : <CardinalMPI> <ReasonCode></ReasonCode> <ErrorDesc></ErrorDesc> <MerchantReferenceNumber></MerchantReferenceNumber>... (4 Replies)
Discussion started by: laknar
4 Replies

5. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

6. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

7. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

8. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

9. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question