Sponsored Content
Top Forums Shell Programming and Scripting Script to replace stings in multiple text files Post 302977326 by SIMMS7400 on Friday 15th of July 2016 10:18:06 PM
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.
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 07:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy