Programming :: Find And Replace Script In Bash?
Sep 28, 2010
Im looking for assistance to create a script to find and replace files.Probably best if I give you the background Our server uses a specific application which stores user data, each user data account (a folder on the server) has a file called 'Profile.xml' this file gets updated and replaced about every 30 mins similar to the fashion logrotate works i.e. Profile.xml.1 Profile.xml.2 -> .10
What we experience is that if the application crashes unexpectedly while it is doing its user profile refresh task we end up with sometimes a few hundred Profile.xml files which end up 0kb(should be around 4kb) , and our server see's these as corrupted profiles and will not see them. Our fix is to go back thru and rename the Profile.xml.1 to be Profile.xml (or sometimes up to Profile.xml.5 to Profile.xml) We want a script we can manually run to automate this process The server tree is
/mnt/array1/username/db/Profile.xml
/mnt/array2/username/db/Profile.xml
etc
etc
What we have so far is a script which finds the affected files
find /mnt/ -maxdepth 4 -name Profile.xml -size -1k
This will display a list of affected profiles, and we can append it to a text file with >>output.txt on the end.
if 'pattern' in 'location' equals '0kb' then 'cp' Profile.xml.1 Profile.xml
View 4 Replies
ADVERTISEMENT
Feb 14, 2011
I wanted to find and replace a string from a perl file. I have written a script in bash which runs the following command.
perl -pi -e "s/$findstring/$replacestring/" testfile
where as $findstring = print F_WC_TMP"$line
";
and $replaceString = $line = join ' ', split ' ', $line; print F_WC_TMP"$line
";
But when I am running the above command, i think it is replacing the $findstring with the above mentioned string and hence it contains a $line, it is looking for the variable $line and not finding the exact string. I am confused about how to search for a string that contains $ in it and replace it with another $string.
View 5 Replies
View Related
Apr 3, 2010
I'm having problems with Tomboy. I have a few hundred note files and I need to go through all of them and replace all instances of "<link:broken>a</link:broken>" with "a". Is there a bash command I can use to do this?
View 2 Replies
View Related
Apr 18, 2009
I have a program I am writing where I have a for loop and I want to make it substitute the variable twice like:
for ((i=0;i<5;i++)) do
echo $"$i"
done
[code]...
View 3 Replies
View Related
Jul 7, 2011
I need a command to search a string in a file and then to convert the next string in the same line from hexadecimal to binary. I was able to put everything in capitals. The original file can be as such:
E 2
C 1 794
T ffff
E 2
C 1 787
It is not always FFFF! I am trying to do this in a file at once, not reading line by line (using while).
View 5 Replies
View Related
Aug 13, 2010
I have a sysctl.conf that has the following in it:
kernel.exec-shield-randomize = 1
kernel.exec-shield = 1
When I grep kernel.exec-shield I get both line, hence I keep over writing the kernel.exec-shield-randomize in my script because it finds them both for my sed commend.
How can I get an exact match with either sed/awk/grep in shell so I can do a find and replace?
Example: sed 's/^kernel.exec-shield =.*/kernel.exec-shield = 1/g' /etc/sysctl.conf will replace BOTH lines
Example: grep "^kernel.exec-shield" find both line and I want it to find only the exact line.
View 3 Replies
View Related
Aug 13, 2010
I have a file with the following in it:
:0 local /usr/X11R6/bin/X -nobanner
:1 local /usr/X11R6/bin/X
I need to add "-nolisten tcp" to both of these lines and cannot figure out how to do it. I can get it to do the first one, but not the 2nd and so on. How do I search the file and loop though it to change them all?
View 14 Replies
View Related
Mar 10, 2009
What is best way to replace the 1 and the 3 below with a 0 using a shell script? It is in a text file with lots of lines and similar lines:
lots of text
...
useHeloBlacklist:=1
DoIPinHelo:=3
...
lots of text
View 5 Replies
View Related
Apr 9, 2010
I am trying following script can I do this or is there a way to do find and replace the replacing word is dynamic input by user
echo -n "Enter name:"
read RP_USER
sed 's/text1/$RP_USER/' /home/user/file1 > /home/user/file2
View 5 Replies
View Related
Apr 26, 2009
I would like to have a script that will do a search in an XML file for a specific block of XML code and replace it with a different block of XML code using the Windows build of GNU Sed 4.1.5.
Here's the text I want to find:
Code:
And here's what I want to replace it with:
Code:
There is however a lot of stuff in there that would mess up sed, and the one example from the sed FAQ (from the Sourceforge site) that looked promising and easy enough to figure out (4.23.3. Try to use a block of "literal strings") fails with this error message: sed: file blockrep.sed line 18: unterminated `s' command
Anyone got any ideas, or even an alternate program that can do this? (Preferably one that I can automate.)
View 4 Replies
View Related
Apr 30, 2010
I'm writing a program in Python that presents a place for the user to input one-line of text (using raw_input). I need, however, as the user types, for certain words to be replaced, even before the user hits enter.
I try to make an animation so you can see what I mean, here the word "pig" gets replaced with "sheep":
>I|
>I |
>I l|
>I li|
>I lik|
>I like|
>I like |
>I like t|
>I like to|
>I like to |
>I like to e|
>I like to ea|
>I like to eat|
>I like to eat |
>I like to eat p|
>I like to eat pi|
>I like to eat pig|
>I like to eat sheep|
Note, the user never typed "sheep", the program replaced "pig" with "sheep" as soon as they finished typing the word "pig". The user did not use "Enter". Is this at all possible in Python?
View 4 Replies
View Related
Mar 11, 2011
I am trying to find a nightly backup if it was successfully copied over, rename it and curl, but it's always passing the check even if the file is older than specified. From the command line it does as it should. Example is here;
Code:
find /backup -type f -mmin +4440 -exec echo "found" {} ;
- nothing returned (good). Then I change the time
[code].....
View 4 Replies
View Related
Oct 11, 2010
I found this command that works great finding and replacing a simple string to another in files located in that folder and all sub-folders.
Code: find . -name '*.php' | xargs perl -pi -e 's/OldText/NewText/g'
The problem I have is that I need to replace a more complex string, like this: Old string: /mnt/stor6-wc2-dfw1/627896/982574/ New string: /mnt/stor8-wc2-dfw1/369587/302589/ There I don't know how to do it... since the / is what separates the old from the new strings, and the strings that I want to replace have / in it. Also, I would like to know how to specify under what folder replace the files, for example, I want that it search/replaces all files under /var/www/mysite/htdocs folder.
View 1 Replies
View Related
May 15, 2011
i had a problem with the find command in bash (which i deem is close enough to a promming language, if not please move this thread :P). i tried to reduce the command to the problem. i want the backticks, or $() for that matter; to be evaluated by -exec of find, not by bash. is that a caveat of find?
Code:
$ find testd -exec echo `basename {}` ; #confused me
test
test/a
test/b
[code]...
edit: i found out whats causing this. `basename {}` gets evaluated by bash before find is invoked, returns {} and `find . -exec echo {} ;" is run. now my question is, how to escape this eveluation from happening before.
View 11 Replies
View Related
Mar 17, 2010
I am writing a script based image manipulator but i need to know if X is running so i can tell if i use CACAVIEW to imagemagick DISPLAY command.
View 14 Replies
View Related
Feb 3, 2011
i want to find all files with .h or .c extension and print them on the screen. How can i do it with bash script programming?
View 5 Replies
View Related
Jul 28, 2009
I have script that I'm working on that updates a username in all the files that are called blah.inc for my framework. since i host a bunch of these web apps i need to do it to all of them. so I need to figure out how to update these files automagically with out me watching it to call vim every time. heres what I have so far
Code:
This finds the files but now i need to figure out how to do s/bob/fred/g on those files.
View 5 Replies
View Related
Jul 12, 2010
I am trying to write a Perl script that can open a file, find text that appears between two identifying strings (for now, "start" and "end"), then modify that text by enclosing it between "term_" and "_term" . Since the identified strings vary, the replacement string becomes "term_$1_term". From looking at other threads in this forum I've been able to get as far as spitting out the modified terms using the following code:
open FILE, "start2.txt" || die ("Could not open file <br> $!");
$text = <FILE>;
while ($text=~ s/start (.*?) end//) {
[code]....
The problem is how to get "term_$1_term" into the file in the same while loop, which I'm guessing would be some of variant of "$text=~ s/$1/$term/;" (which doesn't work as it stands).
View 6 Replies
View Related
Apr 17, 2011
write such script (bash script). I have some text file with name filename.txt I must check if this file contains string "test-string-first", I must cut from this file string which follows string "keyword-string:" and till first white-space and save it to some variable.
For example. File: PHP Code: PHP Code:
Start 15022011 Eng 12-3-42
SN1232324422 11 test-string-first
SN322211 securities
HH keyword-string:123456321-net mark (11-22)
[Code].....
View 1 Replies
View Related
Feb 25, 2010
I'm working on a bash script that will go through a directory, find the sub-directories that have been created since the last time the script ran, count the results, and output that integer (will most likely be '1' or less per each instance run) to a file. Give the circumstances, my previous (and very limited) experience with bash is not sufficient for me to pull this off. since it probably has bearing, is that my mail server stores files that it flags as viruses in a folder. It creates a sub-directory for each virus that it quarantines .I want to count those subdirectories and graph them with MRTG. Hence the script. I'm going to post what I've got so far and the purpose of it, because I'm told I have a very odd and efficient way of doing scripting.
[Code]...
But then it dawned on me that it wouldn't work because I would have to not count the directories that have already been counted and count the ones that have not been counted. Given that the purpose of this is to generate a graph about every 5 minutes, using find won't work because, to my knowledge, that will only find things based on whole day values, I need it almost down to the minute.
View 1 Replies
View Related
Oct 13, 2010
This may be a basic bash array/string operation related question, but I couldn't find any direct answer. So here it goes:I have a lot of data sorted in various directories. All directories need same processing except for a special group of directories. I have a symbolic link of the script in discussion in each directory. I want the script to get the name of the current directory, check if that belongs to special group and do specific operations.So I get the name of the directory
Code:
mm=`basename `pwd``
Now the the group of directories that needs something different to be done, contains these
[code]...
View 4 Replies
View Related
Dec 5, 2010
I'm starting bash shell script and I'm looping without any solution.
I'm trying to find some files under a folder hierarchy and in case of errors moving these files to a destination folder under the same hierarchy recreating this hierarchy if not exists.
Finding all ._* files under /src and moving them to /dest recreating folder1 or the others which contains ._* files but without moving files which does not correspond to the pattern.
Code:
I tried find command and I'am getting all needed files
Code:
But I don't know how to use the output to get the parent folder of files which are found to
1- create folder with mkdir -p /dest/folder1 or /dest/folder1/folder4
2- move found files from /src/... to /dest/... with rm command
I'm working on a find command as this trying to do all in the same line but ... little lost
Code:
View 8 Replies
View Related
Jun 5, 2009
I want to remove duplicate or multiple similar lines from multiple files. I.e. if I have four files file1.txt file2.txt file3.txt and file4.txt and would like to find and remove similar lines from all these files keeping only one line from these similar lines. I only that uniq can be used to remove similar lines from a sorted file.
View 9 Replies
View Related
Aug 17, 2010
I'm learning Bash and can't find how to say "if file1 exists and file2 exits then...".I thought this may work:
Code:
file1="/root/file1"
file2="/root/file2"
[code]....
View 5 Replies
View Related
Jul 8, 2009
I use command "find" in my bash script: if the filename exist command find work quiet, and if the filename not exist I see the message "find: /tmp/filename: No such file or directory". My problem is following, i want to have in my script something like this:
find "/tmp/filename" -type f -delete | "if no_any_errors execute command1" , if file_not_found execute command2"
View 1 Replies
View Related
Mar 9, 2011
Almost by mistake, I figured out you could do something like:
$ cp foo.data bar1.data
$ ^bar1^bar2
And that runs the same command with bar2.data instead of bar1.data. Now, how about if I have multiple occurrences of the target word? For example:
$ cp foo.data bar.data
$ ^data^index
It only replaces the first data extension. How do I get it to replace both?
View 1 Replies
View Related
Mar 10, 2011
I have something like...
Code:
var=beer
# echo ${var/%e/E} doesn't do anything because i can only replace "r" or "er" this way
[code]....
View 1 Replies
View Related
Jun 17, 2010
What i want to do is pretty simple.I want to uncomment every line that begins with "deb" (except for deb cdrom) in /etc/apt/sources.list.I know how to do this through system > administration > software sources.I know I can gksu gedit /etc/apt/sources.list.I'd rather not do it that way.I'd rather have a script do it. It's less work, less typing, less clicking, and would work the same on every ubuntu version.
View 8 Replies
View Related
Dec 27, 2010
I am working on writing a script for find and replace a string using sed.But know I want to write without using sed.
View 9 Replies
View Related
Jul 26, 2011
I need to find this "TYPE=MyISAM;" and replace with "ENGINE=MyISAM;" in all .sql files in a directory. How can I do this with sed?
View 2 Replies
View Related