Use of sed (linux)? - sed

I'm trying to replace all strin $PHP_SELF by $_SERVER['PHP_SELF'] in files index.php... But it seems that my syntax is wong:
sed -i 's/\$PHP_SELF/\$_SERVER\[\'PHP_SELF\'\]/g' index.php
Any idea please?
Thanks

sed -i .bak "s/\\\$PHP_SELF/\$_SERVER['PHP_SELF']/g" index.php

Related

how to use sed to delete the path?

The contents of file:
/usr/local/foo/test
/usr/local/foo/testlog
how to use sed to delete one of them?
FSNAME=/usr/local/foo/test OR /usr/local/foo/testlog
sed -i "s#${FSNAME}#EXCLUSIVE#;/\<EXCLUSIVE\>/d" /tmp/file
this does not work, please help me?
thks
You can try the following code:
FSNAME=$(echo "/usr/local/foo/test /usr/local/foo/testlog" | xargs shuf -n1 -e)
sed -i.bak "s:$FSNAME:EXCLUSIVE:;/EXCLUSIVE/d" file
the shuf command will allow you to choose randomly between the 2 paths,
then the sed command will delete the line containing only this path.
test (without editing the file)

How to copy and paste sample ID to end of header

I have a fasta file with headers that look like...
>DNA1111_0
>DNA2987_1
>DNA3674_5
How do I used sed to modify the headers so they look like...
>DNA1111_0;sample=DNA1111
>DNA2987_1;sample=DNA2987
>DNA3674_5;sample=DNA3674
I haven't been able to get the correct modification, thank you.
With GNU sed:
sed -E 's/^>(.*)(_.*)$/>\1\2;sample=\1/' file
Output:
>DNA1111_0;sample=DNA1111
>DNA2987_1;sample=DNA2987
>DNA3674_5;sample=DNA3674
With any sed that supports -E (e.g. GNU and OSX seds):
$ sed -E 's/([^>_]+).*/&;sample=\1/' file
>DNA1111_0;sample=DNA1111
>DNA2987_1;sample=DNA2987
>DNA3674_5;sample=DNA3674

How to replace a string using Sed?

Suppose I have a string like this
<start><a></a><a></a><a></a></start>
I want to replace values inside <start></start> like this
<start><ab></ab><ab></ab><ab></ab><more></more><vale></value></start>
How do I do this using Sed?
Try this :
sed 's#<start>.*</start>#<start><ab></ab><ab></ab><ab></ab></start>#' file
I get this line with gnu sed :
sed -r 's#(<start>)(.*)(</start>)#echo "\1"$(echo "\2"\|sed "s:a>:ab>:g")"\3"#ge'
see example:
kent$ echo "<start><a></a><a></a><a></a><foo></foo><bar></bar></start>"|sed -r 's#(<start>)(.*)(</start>)#echo "\1"$(echo "\2"\|sed "s:a>:ab>:g")"\3"#ge'
<start><ab></ab><ab></ab><ab></ab><foo></foo><bar></bar></start>
note
this will replace the tags between <start>s which ending with a . which worked for your example. but if you have <aaa></aaa>:
you could do: (I break it into lines for better reading)
sed -r 's#(<start>)(.*)(</start>)
#echo "\1"$(echo "\2"\|sed "s:<a>:<ab>:g;s:</a>:</ab>:g")"\3"
#ge'
e.g.
kent$ echo "<start><a></a><a></a><a></a><aaa></aaa><aba></aba></start>" \
|sed -r 's#(<start>)(.*)(</start>)#echo "\1"$(echo "\2"\|sed "s:<a>:<ab>:g;s:</a>:</ab>:g")"\3"#ge'
<start><ab></ab><ab></ab><ab></ab><aaa></aaa><aba></aba></start>
sed 's/(\<\/?)a\>/\1ab\>/g' yourfile, though that would get <a></a> that was outside <start> as well...
grep -rl 'abc' a.txt | xargs sed -i 's/abc/def/g'

Replace pattern in an existing line using sed

I want to replace the
/fdasatavol/ankit
to
/fdasatavol_sata/ankit
Can anyone help me out in this?
to write to a new file (without modifying file1):
sed 's/fdasatavol/fdasatavol_sata/g' file1 > file2
or to replace in the original file:
sed -i 's/fdasatavol/fdasatavol_sata/g' file1
This will replace each occurrence of fdasatavol with fdasatavol_sata:
sed 's/fdasatavol/&_sata/g'
If your input has occurrence of fdasatavol that are not in /fdasatavol/ankit and you don't want to substitute these then use:
sed 's#/fdasatavol/ankit#/fdastatavol_sata/ankit#g'
Note: you can use any character as sed's delimilter to aviod the confusion with the parrtern contiaing /. sed prints to stdout by default, if you are happy with the changes produced by sed you can use the -i option to store back to the file.
sed -i 's/fdasatavol/&_stat/g' file

Remove string/script from all files (recursive)

One of my websites has been hacked, all the index.html and index.php files have been infected with a certain Javascript. I would like to have a unix command to remove this script from all files.
Script is here: http://pastie.org/private/6osrvd5zhphe372gblrc6w
I am trying to figure this out with sed but no luck so far
Thanks!
sed -i 's/<script>.*<\/script>//' fileName
will remove the tag script and all its content.
This works if you only have one <script> tag.
If you haven't only one, extend it with try keyword in the following way
sed -i 's/<script>try.*<\/script>//' fileName
Edit
If you want to do it on all files in a recursive way, you can use a find command like this:
find . -name "index.html" -print | xargs sed -i 's/<script>try.*<\/script>//' fileName
where . is the current directory
You can try this
find src/ -name "index.html" -print | xargs sed -i 's/<script>try{document.body++}catch(dgsgsdg){zxc=12;ww=window;}if(zxc).*<\/scri‌​pt>//
perl -pi -e 's/<script>.*<\/script>//g' index.html