sed to substitute words in a file [duplicate] - unix

This question already has answers here:
In-place edits with sed on OS X
(8 answers)
Closed 6 years ago.
I try to learn Unix and how to use the terminal. I am in a Mac
I open the terminal, I go to a folder. Inside there is the file fruit.txt. In that file there are only the words pear, apple
I want to substitute the word pear and put mango in the file fruit.txt
sed 's/pear/mango/' fruit.txt
the terminal gives me:
mango, apple
But I open the file and nothing changed. What am I missing?

sed is a stream editor. This means that it reads data, changes it,
and sends the result to output. It doesn’t automatically replace the
contents of a file.
sed -i or sed --in-place will replace the file as you want.
Alternatively, you can do something like this:
sed fruit.txt > newfruit.txt
and the output will go to a new file. This is safer; if you make a
mistake, you still have your original file.

Related

unix sed and redirection [duplicate]

This question already has answers here:
Find and replace in file and overwrite file doesn't work, it empties the file
(12 answers)
Closed 6 years ago.
I am trying to modify a file using sed in a linux server (Ubuntu 16.04).
Here is an example of the code I am running:
sed 's/lineToChange/newString/' example.txt > example.txt
I feel like I should see newString in example.txt after executing this command since the result of the sed command (which prints newString when executed by itself without the redirect) is redirected to overwrite the example.txt content.
Unfortunately the file ends up empty when I do this...
My common sense is telling me that this should be right but clearly there is something I just don't understand here.
If you want to edit a file inline, you should use the -i option:
sed -i 's/lineToChange/newString/' example.txt
This runs sed into a new file and moves that file to example.txt. Whenever you do ">", you essentially empty out example.txt which makes it empty for sed to work on.

Delete a line using SED from a file and redirect the output to same file

I use sed command to delete first two lines and last line from a file and i want to redirect the output to the SAME file
EXAMPLE :
sed '1,2d' '$d' pra >pra
this deletes first two lines & last line from the file .But when i try to redirect the output to Same file , an Problem Occurs .
when i open and see the the redirected file pra nothing is present inside the file .
IS there a better way of doing this ..deleting first two lines and last line from a file and and saving it with the same file Name .
To save output to same file, use --in-place or -i modifier.
E.g.
sed -i options_you_want name_of_file_to_change
It's much neater than redirecting to temp files, and then back to original file.
As pointed out below, --in-place may not be available on Solaris...
It is not possible on Solaris 10, unless you have GNU sed installed as an optional package. Anyway it is safest to use an intermediate file and use the -i option with a backup file.
On Solaris 10 you could do this instead:
sed '1,2d; $d' file > file.new && mv file.new file

N command in sed

The 'N' command in sed works differently with Cygwin's sed(GNU sed I think) & AIX's sed.
$cat > input
Apple
$cat input
Apple
$sed 'N' input
$cat > input
Apple
Orange
$sed 'N' input
Apple
Orange
$
As seen above, the first sed 'N' input command printed nothing for AIX's sed as there was no new input line. However Cygwin's sed printed Apple for the same.
Can some unix/sed guru throw some light into this? Thanks in advance.
FWIW, I just found this behavior has been documented here:
http://sed.sourceforge.net/sedfaq6.html#s6.7.5
It looks like AIX is behaving correctly, as per the POSIX standard (my italics):
[2addr]N
Append the next line of input, less its terminating < newline >, to the pattern space, using an embedded < newline > to separate the appended material from the original material. Note that the current line number changes.
If no next line of input is available, the N command verb shall branch to the end of the script and quit without starting a new cycle or copying the pattern space to standard output.
This is from http://pubs.opengroup.org/onlinepubs/009695399/utilities/sed.html.
So, you've probably found a bug (or a least a non-conformance to POSIX) in GNU sed.

find and replace using sed in unix [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use slashes in sed replace
I need to find the following string /a/b/c and replace it with /r/s/t
It is a string and not a folder
/sam/pam/nancy --> /tim/cook/iphone
I am in the directory and just need to update multiple files having this line.
Use sed to change the files in-place. For example:
sed -i 's|/a/b/c|/r/s/t|g' *.txt
perl -pi -e 's/\/a\/b\/c/\/r\/s\/t/g' file_name

sed -i option is not working on solaris

I am using sed to replace a line with NULL in a file. The command i used is
sed -i "s/.*shayam.*//g" FILE
This is working fine in linux. shayam is replaced with blank in the FILE. But when i used this in solaris it is showing some error.
sed: illegal option -- i
How to use -i functionality of sed in solaris. Kindly help.
The -i option is GNU-specific. The Solaris version does not support the option.
You will need to install the GNU version, or rename the new file over the old one:
sed 's/.shayam.//g' FILE > FILE.new && mv FILE.new FILE
I just answered a similar question sed -i + what the same option in SOLARIS, but for those who find this thread instead (I saw it in the related thread section):
The main problem I see with most answers given is that it doesn't work if you want to modify multiple files. The answer I gave in the other thread:
It isn't exactly the same as sed -i, but i had a similar issue. You
can do this using perl:
perl -pi -e 's/find/replace/g' file
doing the copy/move only works for single files. if you want to
replace some text across every file in a directory and
sub-directories, you need something which does it in place. you can do
this with perl and find:
find . -exec perl -pi -e 's/find/replace/g' '{}' \;
sed doesn't haven an -i option.
You are probably using some vendor-specific variant of sed. If you want to use the vendor-specific non-standardized extensions of your vendor-specific non-standardized variant of sed, you need to make sure that you install said vendor-specific non-standardized variant and need to make sure that you call it and don't call the standards-compliant version of sed that is part of your operating environment.
Note that as always when using non-standardized vendor-specific extensions, there is absolutely no guarantee that your code will be portable, which is exactly the problem you are seeing.
In this particular case, however, there is a much better solution: use the right tool for the job. sed is a stream editor (that's why it is called "sed"), i.e. it is for editing streams, not files. If you want to edit files, use a file editor, such as ed:
ed FILE <<-HERE
,s/.shayam.//g
w
q
HERE
See also:
Unable to use SED to edit files fast
How can I replace a specific line by line number in a text file?
Either cat the file or try <?
Then pipe (|) the result to a temp file and if all goes well (&&) mv the tempfile to the original file.
Example:
cat my_file | sed '!A!B!' > my_temp_file && mv my_temp_file my_file

Resources