I edited the threads title to correct the typo ("substite"-> "substitute", "charather" -> "character").
Please note that the -i switch is NOT part of the standard sed and is only understood by GNU-sed. In addition i strongly suggest NOT to use it even if it is available:
Basically sed always relies on a second file for output because it cannot work how it does work (that is: as a pipeline) in any other way. The -i switch does not change that at all, it just makes sed do it "behind the curtain". In fact a second (temporary) file is created and then moved over the original file.
The problem with this is that: the inode number (and all the other metadata) of the file is changed, of course. If you do it manually as i suggested this is obvious because you obviously have a new file with which you replace the old one. With the -i switch you seem to have the same file - but you don't! So, if you rely on the file ownership or the filemode staying constant but have a directory with sticky bits set or if you rely that the inode number stays constant you are going to be disappointed.
The even bigger problem (IMHO) is that a temporary file gets created but - unlike with the standard way of doing things - you have no control over the process. In case of an error the temporary file might stay, wasting space. Or it may be created in a filesystem where you don't expect it and this filesystem may be too small. Or....
As a last concern i'd like to offer: it is not portable. The way i sketched out above will work everywhere. The method using the -i swithc will only work on select systems. This usually will not pop up for years but the incompatibility will raise its ugly head in the moment you need that least - and it will inadvertently bite you in the behind.
(Sad truth: this will happen anyway sooner or later. If you can minimise the opportunities it can happen it is likely to hurt a bit less - and i for my part am pretty particular when it comes to parts of me i like to use for sitting.)
I hope this helps.
bakunin
I hope this helps.
Last edited by bakunin; 03-14-2019 at 01:21 PM..
These 2 Users Gave Thanks to bakunin For This Post:
I am having a problem executing a sed substitute in a file. I have tried alot of different things I found in previous posts, however non seem to work.
I want to substitute this in $FILE:
VALUE=33.4
In the script I have tried the following:
prev=$(awk -F"=" '{ print $2 }' $FILE )
new=$(echo... (16 Replies)
im having trouble doing this:
i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba
is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's?
im guessing its like getting the 1's compliment of the string
im doing this in... (2 Replies)
Hi All,
I'm currently using SED to make various changes to some .xml files I'm working on, but I'm stuck on this particular problem.
I want to remove '<placeholder>element-name</placeholder>' from the following:
<heading>Element <placeholder>element-name</placeholder> not... (2 Replies)
i am a newbie to shell script,so i want a kshell script in which i need to check for a particular character inside a file through conditional looping(like if ,case,while)and if that character exists ,then substitute a given character to that character.
consider a file test.txt,inside the file... (1 Reply)
I am trying to get rid of some ending tags but I run into some problems.
Ex.
How are you?</EndTag><Begin>It is fine.</Begin><New> Just about
I am trying to get rid of the ending tags, starts with </ and ending with >. (which is </EndTag> and </Begin>)
I tried the following
sed... (2 Replies)
I'm using sed to perform a simply search and replace. The typical data is:
<fig><image href="Graphics/BAV.gif" align="left" placement="break"
I need to replace the value in the first set of quotes, keeping the remainder of the line the same. Thus:
<fig><image href="NEW_VALUE" align="left"... (3 Replies)
Hi All,
Here is what I'm trying to do with sed:
Input File:
somechangeVariable1=Something I would like to change
somechangeVariable2=Something else I would like to change
...
Output File:
somechangeVariable1=Something I would like to different
somechangeVariable2=Something else I would... (6 Replies)
Is there a way to substitute the URL-encoding references of ( & and ` ) with their actual appearance? for example....
%26 is &
say I want to convert every %26 in my file to &.....
awk '{gsub(/%26/,"&");print}'
Is there a way to do this?
I also want to be able to convert ` too! (3 Replies)
Hi ,
I have input file like below
Hi this is "vinoth".
Hi happy to work with 'unix'
USA(united states of America)
My script variables are below :
Dquote=Ộ
Squote=&#$567
Obrac=&^986
Cbrac=&^745
I want to read the variables in my SED command to replace the double quote,single... (9 Replies)
I am trying to do what I thought should be a simple substitution, but I can't get it to work.
File:
Desire output:
I thought I'd start with a sed command to remove the part of the header line preceding the string "comp", then go on to remove the suffix of the target string (e.g. ":3-509(-)"),... (3 Replies)