Search and replace string in files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace string in files
# 1  
Old 03-24-2011
Search and replace string in files

I'm trying to remove the following string from several files.
Code:
<img heigth="1" width="1" border="0" src="http://myteenmovies.net/t.php?id=5540372">

I'm using the following script
Code:
#!/bin/bash
#    This script will search and replace all regular files for a string 
#    supplied by the user and replace it with another string. 
#    
#    Written by Daniel McCarthy
#    daniel.mccarthy@linuxphile.org
#
function usage {
  echo ""
  echo "Search/replace script"
  echo "    Written by Daniel McCarthy"
  echo "      daniel.mccarthy@linuxphile.org"
  echo "      http://linuxphile.org"
  echo ""
  echo "Not enough parameters provided."
  echo "Usage: ./$0 replacestring"
  echo ""
}

#check for required parameters
 if  [ ${#1} -gt 0  ];
 then
 for f in `find  -type f`;
 do
  if grep -q $1 $f;
  then
   cp $f $f.bak.replace
   echo "The string $1 will be replaced in $f"
   sed s/$1//g < $f.bak.replace > $f
  fi
 done
 
 else
 #print usage informamtion 
 usage
fi

I have tried the following, but no luck.

Code:
./replace.sh '\<img heigth\=\"1\" width\=\"1\" border\=\"0\" src\=\"http:\/\/myteenmovies\.net\/t\.php\?id\=5540303\"\>'

Thank you
# 2  
Old 03-24-2011
Use

Code:
sed -i -e 's/<img heigth="1" width="1" border="0" src="http:\/\/myteenmovies.net\/t.php\?id=5540372">//g' file*


Last edited by un1xl0ver_rwx; 03-24-2011 at 03:55 PM..
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. 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

4. Shell Programming and Scripting

String search and replace in multiple files.

Hello. I have five config files in /etc that I want to edit in one click for testing. I would like to make a script like this : #!/bin/bash # a_file="/etc/file_1" src_str="src_string_1" rpl_str="rpl_string_1" calling_sed_or_awk_or_whatelse $a_file search_for_all $src_str replace_with... (4 Replies)
Discussion started by: jcdole
4 Replies

5. 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

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

find and replace a search string recursively in files

Hi , I have a directory structure as dir and subdirectories and files under it and so on.now I need to find the files which contain the search string under every dir and subdir and replace . my search string is like searchstring=/a/b string to be replaced=/a/c/b please help. ... (7 Replies)
Discussion started by: mohanpadamata
7 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