Script to replace stings in multiple text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to replace stings in multiple text files
# 1  
Old 07-15-2016
Script to replace stings in multiple text files

Good Evening Folks -

Happy Friday!

I have a need to replace a certain string in all .csv files from "0.00" to "#Missing" in my /app/hyp_app/files directory.

Does anyone have a script they use regularly that's rather quick in performance? My files are rather large so I'm looking for a quick and efficient solution.

Thank you all!
# 2  
Old 07-15-2016
This can be done fairly quickly with sed, but without a clearer description and representative sample input and corresponding sample output, I wouldn't want to hazard a guess at code that might do what you really want.
  1. What operating system are you using?
  2. What shell are you using?
  3. What have you tried to solve this problem on your own?
  4. What is the field delimiter in your .csv files?
  5. Does this string only appear once in each file, or does it appear on multiple lines, or does it appear multiple times on a single line, or multiple times on multiple lines?
  6. Do you want to make this change everywhere in a file or only on certain lines and/or only in certain fields?
  7. Is the text you want to change 0.00 or "0.00"?
  8. Is the replacement text supposed to be #missing or "#missing"?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 07-15-2016
This is what I have pieced together but I dont like it. Not pretty.

Code:
!/bin/bash  
# **************** Change Variables Here ************  
startdirectory="/app/hyp_app/files/essbaseimport"  
searchterm=0.00  
replaceterm=#Missing
# **********************************************************  
  
echo "******************************************"  
echo "* Search and Replace in all .csv files *"  
echo "******************************************"  
  
        for file in $(grep -l -R $searchterm $startdirectory)  
          do  
           sed -e "s/$searchterm/$replaceterm/ig" $file > /files/tempfile.tmp  
           mv /files/tempfile.tmp $file  
           echo "Modified: " $file  
        done  
  
echo "DONE"

  1. What operating system are you using? Linux
  2. What shell are you using? /bin/sh
  3. What have you tried to solve this problem on your own? See above
  4. What is the field delimiter in your .csv files? comma
  5. Does this string only appear once in each file, or does it appear on multiple lines, or does it appear multiple times on a single line, or multiple times on multiple lines?multiple times on multiple lines
  6. Do you want to make this change everywhere in a file or only on certain lines and/or only in certain fields? everywhere
  7. Is the text you want to change 0.00 or "0.00"? 0.00
  8. Is the replacement text supposed to be #missing or "#missing"? #Missing



Quote:
Originally Posted by Don Cragun
This can be done fairly quickly with sed, but without a clearer description and representative sample input and corresponding sample output, I wouldn't want to hazard a guess at code that might do what you really want.
  1. What operating system are you using?
  2. What shell are you using?
  3. What have you tried to solve this problem on your own?
  4. What is the field delimiter in your .csv files?
  5. Does this string only appear once in each file, or does it appear on multiple lines, or does it appear multiple times on a single line, or multiple times on multiple lines?
  6. Do you want to make this change everywhere in a file or only on certain lines and/or only in certain fields?
  7. Is the text you want to change 0.00 or "0.00"?
  8. Is the replacement text supposed to be #missing or "#missing"?

Last edited by Don Cragun; 07-15-2016 at 11:32 PM.. Reason: Add CODE tags.
# 4  
Old 07-17-2016
Code:
sed --in-place 's/0.00/#Missing/g' /app/hyp_app/files/*.csv

# 5  
Old 07-18-2016
Also you can use a while loop that correctly handles special characters in file names, and proper "quoting"
Code:
        grep -l -R "$searchterm" "$startdirectory" |
        while IFS= read file
        do  
           sed -i -e "s/$searchterm/$replaceterm/ig" "$file"
           echo "Modified:  $file"
        done

Remember that searchterm may not contain the delimiter / that you use in sed; you can take another delimiter if needed.
# 6  
Old 07-18-2016
Hi SIMMS7400,
The basic idea behind your script is OK, but there are a couple of holes in the logic: your search patterns aren't anchored and, in both a BRE and in an ERE, the <period> in the $searchterm value 0.00 will match any character; not just a <period>. Therefore, 0.00 will not only match ,0.00,, it will also match ,10200.56, and ,50.00,.

Making a few assumptions (there are always two characters after the decimal point in all of the numbers you want to process, you only want to replace numbers in cases where the entire number is matched by $searchterm, and your code will run slightly faster if there are fewer directories to process in your pathnames) and building on top of MadeInGermany's suggestion, you might want to consider the following as a little bit slower, but safer (it won't change strings in the middle of larger numbers instead of or in addition to the strings you intended to change) way to do what you want:
Code:
#!/bin/bash 
# **************** Change Variables Here ************ 
startdirectory="/app/hyp_app/files/essbaseimport" 
searchterm="0[.]00"
replaceterm="#Missing"
# ********************************************************** 

echo "******************************************" 
echo "* Search and Replace in all .csv files *" 
echo "******************************************" 
  
cd "$startdirectory"
grep -E -l -R "(^|,)$searchterm" . |
while IFS= read -r file
do	sed -i -e "s/^$searchterm/$replaceterm/" \
	    -e "s/,$searchterm/,$replaceterm/g" "$file" &&
	printf 'Modified: %s\n' "$file"
done  
echo "DONE"

Note that the read -r and the printf instead of echo provide extra protection in cases where one of your pathnames might contain a backslash character.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to generate adler32 stings that convert into hex stings in python 2.7?

I want to generate adler32 stings that converts into hex stings in python 2.7 (1 Reply)
Discussion started by: bigvito19
1 Replies

2. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

3. Shell Programming and Scripting

Replace text in SPECIFIC multiple files

I have a list of files with different file names and ext that i need replace(saved as file_list.txt.) i just want to replace from a specific file list. i obtain an error saying lint not found. Plz help. Thx perl -e "s/$NAME/$T_NAME/gi;" -pi $(find . -type f | xargs -0 lint -e < file_list.txt) (0 Replies)
Discussion started by: yvmy
0 Replies

4. Shell Programming and Scripting

Replace text block in multiple files

I need to replace (delete) a text block in a bunch of files, its a html table, almost at the end of pages but the location varies. In Windows I used Filemonkey, but nothing like that in Unix? There is replace from mysql, but how does it deal with newlines? sed only works with single lines,... (6 Replies)
Discussion started by: eiland
6 Replies

5. Shell Programming and Scripting

deleting text between two stings

example.txt ---------- this is a line i want to keep this is another line I wish to keep I wish to delete from here ON until I see four new lines from here and then I wish to keep the rest. These are some special charcters {)#@ which have to be deleted too This is a one more new line... (4 Replies)
Discussion started by: jville
4 Replies

6. UNIX for Dummies Questions & Answers

replace text in multiple files

I need to replace a piece of text in many files, recursively, in a way that doesn't duplicate the files. How would I do that? The closest I've come is grep -rl "text" * | sed -e 's/home1/home2/g' but that just replaces the filename. (2 Replies)
Discussion started by: dhinge
2 Replies

7. Shell Programming and Scripting

Replace text in multiple files

Dear all My task is to replace a strings in multiple files. filename: file1 I can use sed to replace abc.server.com to unix.server.org e.g. sed 's/abc.server.com/unix.server.org/g file1 > newfile1 I have 2 questions. How do I directly save file1 instead of append to newfile1. I... (1 Reply)
Discussion started by: on9west
1 Replies

8. Shell Programming and Scripting

Replace text in multiple files

Ok guys, If anyone could help me out on this puppy I'd be very appreciative! Here's the scenario I have a string for example : <img src=BLANK_IMG border=0 width=221 height=12> or <img src=IMG border=0 height=12 width=221 > or anything else really.... need to basically change each... (10 Replies)
Discussion started by: Tonka52
10 Replies

9. Shell Programming and Scripting

Find and Replace in multiple files (Shell script)

hi guys, Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin (13 Replies)
Discussion started by: jatins_s
13 Replies

10. UNIX for Advanced & Expert Users

Please Help. Need Help searching for multiple stings in a file and removing them.

Please help. Here is my problem. I have 9000 lines in file a and 500,000 lines in file b. For each line in file a I need to search file b and remove that line. I am currently using the grep -v command and loading the output into a new file. However, because of the size of file b this takes an... (2 Replies)
Discussion started by: mjs3221
2 Replies
Login or Register to Ask a Question