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.
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.
What operating system are you using?
What shell are you using?
What have you tried to solve this problem on your own?
What is the field delimiter in your .csv files?
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?
Do you want to make this change everywhere in a file or only on certain lines and/or only in certain fields?
Is the text you want to change 0.00 or "0.00"?
Is the replacement text supposed to be #missing or "#missing"?
This User Gave Thanks to Don Cragun For This Post:
This is what I have pieced together but I dont like it. Not pretty.
What operating system are you using? Linux
What shell are you using? /bin/sh
What have you tried to solve this problem on your own? See above
What is the field delimiter in your .csv files? comma
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
Do you want to make this change everywhere in a file or only on certain lines and/or only in certain fields? everywhere
Is the text you want to change 0.00 or "0.00"? 0.00
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.
What operating system are you using?
What shell are you using?
What have you tried to solve this problem on your own?
What is the field delimiter in your .csv files?
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?
Do you want to make this change everywhere in a file or only on certain lines and/or only in certain fields?
Is the text you want to change 0.00 or "0.00"?
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.
Also you can use a while loop that correctly handles special characters in file names, and proper "quoting"
Remember that searchterm may not contain the delimiter / that you use in sed; you can take another delimiter if needed.
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)