why does it work with sed -e but not with sed -i - bash

I have this in a file
echo "[[z[z[[e" > toto_test.txt
i'm trying this and things are ok
sed -e 's,\],,g' -e 's,\[,,g' toto_test.txt
Doing this and suddenly, it is not working anymore
sed -i 's,\],,g' -i 's,\[,,g' toto_test.txt
I have this error
sed: impossible to read s,\[,,g: No such file or directory
Why and how can I overcome the thing?
Thanks.

try
sed -i -e 's,\],,g' -e 's,\[,,g' toto_test.txt
-i means inplace modification,
-e means expression.
once you understand this, it's easy to provide proper parameters.

You don't need 2 replacements. Just use it as:
sed -i.bak 's,[][],,g' toto_test.txt

so far as I know, -i can't be repeated. It expect an optional backup suffix for the file edited in place (mandatory on OS X).
Try
sed -i 's,\],,g; s,\[,,g' toto_test.txt
(wanted to make this a comment but I don't seem to be allowed to do so, so I made it an answer)

Related

looping the sed -i to delete.

I have a file called foo.file.
In it are many things. I wanted to get rid of two lines containing
these keywords. employee.csv and instrument.csv. The sed -I flag is powerful feature in sed command. I like it to use on edit files in place.
It works just fine when I use in in command line format
sed -i '/employee.csv/d' foo.file
but when I try to loop the keywords. It does not work.
for i in employee.csv instrument.csv ;
do
sed -i '/"$i"/d' foo.file ;
done
~
for i in "employee.csv" "instrument.csv"; do
sed -i '/'"$i"'/d' foo.file;
done
You can also use regular expressions with sed:
sed -ri '/(employee|instrument).csv/d' foo.file;
Using sed to make an in-place substitution change:
for i in employee.csv instrument.csv; do
do sed -i "s/$i//g" foo.file;
done

How to use flag with 'sed' on mac?

I dont find the way to use flag with sed and matching pattern.
I'm trying to us the i flag. But I don't understand how it works.
$ sed -i '' -n '/xxx.xxx#xxx.fr/i d' res.txt
sed: 1: "/xxx.xxx#xxx.fr/i d": command i expects \ followed by text
So I want to match xxx.xxx#xxx.fr And XXX.XXX#XXX.FR
The -i '' is only for --in-file (without cache) the d is for delete.
So how can I use flag and eventually multiple of them ? In the documentation I've found it was that way but it seems not to work at all.
I would use Perl - its regexes and options are far more orthogonal and consistent than all the sed versions across platforms:
perl -i -ne '/XXX.XXX.fr/i || print' res.txt
-i means "in-place" editing
-n means execute a loop around input lines like awk or sed
-e means execute following script

BackSpace in .bash file

I work with .bash script and I try to remove lines from file
sed -e s/^DNS1.*/''/g -i $DNS_IP_CONFIG_FILE
but remains blank lines.I need baskspace in this code
sed -i '/^DNS1.*/d' $DNS_IP_CONFIG_FILE
sed -i -e '/^DNS1/d' "$DNS_IP_CONFIG_FILE"
sed -i -ne '/^DNS1.*/!p' $DNS_IP_CONFIG_FILE

In-place edits with sed on OS X

I'd like edit a file with sed on OS X. I'm using the following command:
sed 's/oldword/newword/' file.txt
The output is sent to the terminal. file.txt is not modified. The changes are saved to file2.txt with this command:
sed 's/oldword/newword/' file1.txt > file2.txt
However I don't want another file. I just want to edit file1.txt. How can I do this?
I've tried the -i flag. This results in the following error:
sed: 1: "file1.txt": invalid command code f
You can use the -i flag correctly by providing it with a suffix to add to the backed-up file. Extending your example:
sed -i.bu 's/oldword/newword/' file1.txt
Will give you two files: one with the name file1.txt that contains the substitution, and one with the name file1.txt.bu that has the original content.
Mildly dangerous
If you want to destructively overwrite the original file, use something like:
sed -i '' 's/oldword/newword/' file1.txt
^ note the space
Because of the way the line gets parsed, a space is required between the option flag and its argument because the argument is zero-length.
Other than possibly trashing your original, I’m not aware of any further dangers of tricking sed this way. It should be noted, however, that if this invocation of sed is part of a script, The Unix Way™ would (IMHO) be to use sed non-destructively, test that it exited cleanly, and only then remove the extraneous file.
I've similar problem with MacOS
sed -i '' 's/oldword/newword/' file1.txt
doesn't works, but
sed -i"any_symbol" 's/oldword/newword/' file1.txt
works well.
The -i flag probably doesn't work for you, because you followed an example for GNU sed while macOS uses BSD sed and they have a slightly different syntax.
All the other answers tell you how to correct the syntax to work with BSD sed. The alternative is to install GNU sed on your macOS with:
brew install gsed
and then use it instead of the sed version shipped with macOS (note the g prefix), e.g:
gsed -i 's/oldword/newword/' file1.txt
If you want GNU sed commands to be always portable to your macOS, you could prepend "gnubin" directory to your path, by adding something like this to your .bashrc/.zshrc file (run brew info gsed to see what exactly you need to do):
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
and from then on the GNU sed becomes your default sed and you can simply run:
sed -i 's/oldword/newword/' file1.txt
sed -i -- "s/https/http/g" file.txt
You can use -i'' (--in-place) for sed as already suggested. See: The -i in-place argument, however note that -i option is non-standard FreeBSD extensions and may not be available on other operating systems. Secondly sed is a Stream EDitor, not a file editor.
Alternative way is to use built-in substitution in Vim Ex mode, like:
$ ex +%s/foo/bar/g -scwq file.txt
and for multiple-files:
$ ex +'bufdo!%s/foo/bar/g' -scxa *.*
To edit all files recursively you can use **/*.* if shell supports that (enable by shopt -s globstar).
Another way is to use gawk and its new "inplace" extension such as:
$ gawk -i inplace '{ gsub(/foo/, "bar") }; { print }' file1
This creates backup files. E.g. sed -i -e 's/hello/hello world/' testfile for me, creates a backup file, testfile-e, in the same dir.
You can use:
sed -i -e 's/<string-to-find>/<string-to-replace>/' <your-file-path>
Example:
sed -i -e 's/Hello/Bye/' file.txt
This works flawless in Mac.
If you need to substitute more than one different words:
sed -i '' -e 's/_tools/tools/' -e 's/_static/static/' test.txt

Text substitution (reading from file and saving to the same file) on linux with sed

I want to read the file "teste", make some "find&replace" and overwrite "teste" with the results. The closer i got till now is:
$cat teste
I have to find something
This is hard to find...
Find it wright now!
$sed -n 's/find/replace/w teste1' teste
$cat teste1
I have to replace something
This is hard to replace...
If I try to save to the same file like this:
$sed -n 's/find/replace/w teste' teste
or:
$sed -n 's/find/replace/' teste > teste
The result will be a blank file...
I know I am missing something very stupid but any help will be welcome.
UPDATE: Based on the tips given by the folks and this link: http://idolinux.blogspot.com/2008/08/sed-in-place-edit.html here's my updated code:
sed -i -e 's/find/replace/g' teste
On Linux, sed -i is the way to go. sed isn't actually designed for in-place editing, though; historically, it's a filter, a program which edits a stream of data in a pipeline, and for this usage you would need to write to a temporary file and then rename it.
The reason you get an empty file is that the shell opens (and truncates) the file before running the command.
You want: sed -i 's/foo/bar/g' file
You want to use "sed -i". This updates in place.
In-place editing with perl
perl -pi -w -e 's/foo/bar/g;' file.txt
or
perl -pi -w -e 's/foo/bar/g;' files*
for many files
The ed solution is:
ed teste <<END
1,$s/find/replace/g
w
q
END
Or without the heredoc
printf "%s\n" '1,$s/find/replace/g' w q | ed teste
Actually, if you use -i flag, sed will copy the original line you edit.
So this might be a better way:
sed -i -e 's/old/new/g' -e '/new/d' file
There is a useful sponge command.
sponge soaks up all its input before opening the output file.
$cat test.txt | sed 's/find/replace/w' | sponge test.txt
Nothing worked for me on MacOS, but after some research I found this answer.
So the following works on MacOS:
sed -i '' -e 's/find/replace/g' teste
However, on Linux distro's (in my pipelines) the following worked and the above command throwed errors:
sed -i -e 's/find/replace/g' teste

Resources