Grumble!! I added comments as I posted it and my correct spelling introduced the bug. Change the comment
so that it doesn't have the single quote (') (make ""we're" "we are" or somesuch).
As for the cd command...
Does the directory "/home/username/" exist, or should username be the name of the user running the script, or did you change it to 'username' so as not to post the real name here? If you really have /home/username, then change username to $USER, or the real name.
There are two schools of thought on the "#!" line. My school of thought is to use #!/usr/bin/env with the parameter ksh, bash, etc. This allows the shell/interpreter to be found using my PATH, and not the hard coded /usr/bin/ksh or whatever is coded. The advantage is that when I have a group of scripts that need to be tested with a particular version of the shell/interpreter, I need only to set up my PATH correctly and the proper version of the interpreter will be invoked for every one of them. I don't need to modify any script to point to the version I am testing under, nor do I need to install the new/beta/old version of the interpreter in /usr/bin or where ever.
The other side of the coin is to hard code the path to the interpreter as you have pointed out. It works, but it is limited in my opnion.
I wrote "username" just to hide the real username, im using the real username instead of it, dont worry.
Ok, heres the script i used:
(my script filename is "newdelete2.ksh" ) Still when i execute it just by typing
it does not work (same problem as before) BUT i tried this
and this
and they both return the following:
I've made a "test" directory (as you can see on the "cd /path/") with those 3 php files in it. footer, home and index.
Unfortunatly nothing happens, the php code was not removed from those files, no code was removed.
Any more ideas ? Thank you! Already helped too much
---------- Post updated 03-05-12 at 11:57 AM ---------- Previous update was 03-04-12 at 01:32 PM ----------
Just found out something, when the PHP block i want to delete is in a position like the one below (notice the "<?php get_header(); ?>" that comes before and on the same line as the "<?php"):
it will only delete the "?>" at the end of the code, and leave the rest of the code intact.
And there are a lot of instances in which the PHP block appears in that position (with the "<?php" appearing in front and right next to a random piece of code, without space between them)
So i edited the PHP test files, and placed the PHP block exactly like the one below:
and it worked, the whole PHP block was removed.
But now, how to make it work when the "<?php" is on the same line, and right next to a random piece of code ?
I figured you dummied in 'username', but I've also learned not to assume!
That's an interesting twist, and here is some revised code that should do the trick. You'll need to supply the unique string in the BEGIN block as it's needed twice; On the off chance that it happens, if the unique string appears inside of an open/close pair that are on the same line, that will be removed.
Hope this works better for you.
As for needing ./scriptname to execute your script, that implies that the current directory is not in PATH. You can add '.' to your PATH or just type the additional './' at the front.
---------- Post updated at 22:33 ---------- Previous update was at 22:10 ----------
Small revision. I realised that if something like this occurs
and the block is dropped, the text before the opening tag is also dropped. This code fixes that bug:
Last edited by agama; 03-05-2012 at 11:25 PM..
Reason: Small typo in the awk marked with edit
Wait, this last script you sent is the full thing ? Or do i need to replace that part ( from "awk" till ' ) in the original script im using ?
I'll be trying it now tho, but please let me know.
Thanks !
EDIT : Replaced the new "awk" code in the script, unfortunatly it didnt work. Same thing happened, only the string that closes the PHP block was moved ( ?> ) , the rest of the PHP block was intact.
Even for the PHP block that completly isolated from other pieces of code, it only removed the ?>
Sorry for the confusion, yes I only pasted the awk portion figuring you could insert that into your script body.
Very strange. I took this to a different machine (FreeBSD) just to see if a different flavour of awk might barf and its only compplaint was to escape the question mark in the following line (new character in red):
The Gnu awk on my Linux host wasn't complaining about that. What version of awk do you have installed?
should give that to you. I've been testing this with GNU Awk 3.1.6.
To test a bit further....
I've cut/pasted the test file I'm using and it doesn't have any issues. I took the awk straight from your post just to be sure and used the dummy "UNIQUE STRING" as well. The result, when I execute it, is the middle section is dropped.
What happens if you save just the awk in a file (lets say test_awk), the data in test_data and try this:
But here's the thing, i tested with these same 3 PHP codes you were testing, and it worked. So the problem is probably with the PHP block im trying to remove.
I will PM you this PHP block so you can test with it, ok ?
Please check your PM box.
---------- Post updated at 09:05 AM ---------- Previous update was at 08:35 AM ----------
Just found out you have PM's disabled. Could you enable it for a second ?
I really can't post this PHP code in here. Let me know!
Hello Guys,
I need to replace a string with multiple lines.
For eg:-
ABC,DEF,GHI,JKL,MNO,PQR,STU
need to convert the above as below:-
ABC,DEF,
GHI1
GHI2
GHI3,
JKL,MNO,
PQR1
PQR2
PQR3,
STU
i have tried using code as:- (2 Replies)
I have a list of files all over a file system e.g.
/home/1/foo/bar.x
/www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g.
if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc.
There... (2 Replies)
Can someone tell me how I can do this?
e.g:
a=$(echo -e wert trewt ertert ertert ertert erttert
erterte
rterter
tertertert
ert)
How do i replace the STRING with $a?
I try this:
sed -i 's/STRING/'"$a"'/g' filename.ext
but this don' t work (2 Replies)
Hi,
I am creating a script to do a find and replace single/multiple lines in a file with any number of lines.
I have written a logic in a script that reads a reference file say "findrep" and populates two variables $FIND and $REPLACE
print $FIND gives
Hi How r $u
Rahul()
Note:... (0 Replies)
find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g'
this is simple logic to find and replace in multiple files & folders
Hope this helps.
Thanks
Zaheer (0 Replies)
I used the following script
cd pathname
for y in `ls *`;
do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y;
done
and it worked fine for finding and replacing strings with names etc. in all files of the given path.
I'm trying to replace a string which consists of path (location of file)
... (11 Replies)
i have to search a string and replace with multiple lines.
example
Input
echo 'sample text'
echo 'college days'
output
echo 'sample text'
echo 'information on students'
echo 'emp number'
echo 'holidays'
i have to search a word college and replace the multiple lines
i have... (1 Reply)
I used the following script
cd pathname
for y in `ls *`;
do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y;
done
and it worked fine for finding and replacing strings with names etc. in all files of the given path.
I'm trying to replace a string which consists of path (location of file)
... (2 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)