Search & Replace

Hi all

Please can you help me with a script to check several files for the following string:

encoding=""

and replace it with:

encoding="UTF-8"

I did the following, :

#!/bin/sh
string1="encoding="""
string2="encoding="UTF-8"
sed 's/'"$string1"'/'"$string2"'/g'

but does not work.

Please can you help

FR

Having $string2 defined as the complete output makes no sense (to me) as you could simply print it without any substitution. I guess this is more appropriate:

$> string1='encoding=""'; string2=UTF-8; echo $string1| sed 's/""/'$string2'/g'
encoding=UTF-8

sed is a StreamEditor and so needs a stream as input to work on. Having no file or redirected input won't make it work.

Original:

#!/bin/sh
string1="encoding="""
string2="encoding="UTF-8"
sed 's/'"$string1"'/'"$string2"'/g'

Needs to be:

#!/bin/sh
string1="encoding=\"\""
string2="encoding=\"UTF-8\""
sed 's/$string1/$string2/g' 

Another thing I don't see here is what stream are you passing to sed?
You have no file name at the end of the sed command and you are not piping anything into the front of it, so sed has no stream to work on.

One way using perl. It modifies your files in-place, use -i.bak to create a backup of each file with 'bak' suffix.

$ perl -i -pe 's/\A(encoding=")(")/$1UTF-8$2/' files

Sorry

I did not get it working!

Do I have to run like:

./mach.ksh <filename.txt

where filename.txt is where the

encoding="UTF-8"

exist?

@birei
sed can modify file in place as well with the "-i" (or --in-place) flag.

sed man page clip:

I changed my post, best try out the actual content of it, thanks.

@fretagi

sed command structure when using a file is:

sed 's/<SEARCH>/<REPLACE>/' <FILENAME>

You have the first part correct just add the filename at the end (without <).

Helpful info can be seen by running:

man sed

Hi

this is the contents of the script:

#!/bin/sh
string1="encoding=\"\""
string2="encoding=\"UTF-8\""
sed 's/$string1/$string2/g' text2insert.txt

this is the text file that needs to be modified:

#more text2insert.txt
<?xml version='1.0' encoding=""?>
<AddressPage xml:lang="PT">

and I am running as this:

sh sandr.sh

but no luck

oops my bad... replace the single quotes in sed with double quotes when using shell variables in search or replace fields:

#!/bin/sh
string1="encoding=\"\""
string2="encoding=\"UTF-8\""
sed "s/$string1/$string2/g" text2insert.txt

Result:

#more text2insert.txt
<?xml version='1.0' encoding="UTF-8"?>
<AddressPage xml:lang="PT">

it does print on the console the wanted results, but does not change the actual file

Check Birei's solution using perl -i or check if your sed supports -i.

it does work when I changed the script to:

#!/bin/sh
string1="encoding=\"\""
string2="encoding=\"UTF-8\""
sed "s/$string1/$string2/g" text2insert.txt > text2.out

but the filename cannot be changed, and is to be used on several files

use the "-i" flag for sed when you get the search and replace correct. When using the "-i" flag do not use the "> text2.out" at the end.

That flag is to change the file "in place" so you do not have to output to another file.

If you ommit the "> text2.out" and do not use the "-i" flag sed will print the changed file to screen so that you can see it.

-i

is not supported on HP-UX.

sed: illegal option -- i
Usage: sed [-n] [-e script] [-f source_file] [file...]

In that case you will have to use the "> newfile.txt" option then copy "newfile.txt" to the old file name.

At least with sed anyway.

Another way would be to adopt the perl method mentioned by birei.

To edit a text file, a text editor seems a natural choice. :wink:

With ex:

ex -sc '%s/encoding=""/encoding="UTF-8"/g|x' file

With ed:

ed -s file <<'EOED'
1,$s/encoding=""/encoding="UTF-8"/g
w
q
EOED

A less readable but equivalent ed one-liner:

printf '%s\n' '1,$s/encoding=""/encoding="UTF-8"/g' w q | ed -s file

On an unrelated and pedantic note, neither GNU's sed -i ... nor perl -i edit anything in place. While those options are convenient, they create and rename a temp file just as you would by using shell redirection and the mv command. To the underlying filesystem, the result is a new file with a different inode. ed and ex, however, actually do edit the file in place.

Regards,
Alister

Another problem is that I have to change that in 30000 files :confused:

hello everyone.

in connection with this thread. i would like to ask if it is possible that I use sed in replacing a string in an entire column? how do i incorporate a specific column, like $2 to the format of sed since it only accepts string

sed 's/'"string1"'/'"string2"'/g'