I'm using sed
for updating my json configuration file in the runtime.
Sometimes, when the pattern doesn't matches in the json file, still sed
exits with return code 0.
Returning 0 means successful completion, but why does sed
returns 0 if it doesnt find the proper pattern and update the file? Is there a workaround for that?
Thanks!
These answers are all too complicated. What is wrong with writing a bit of shell script that uses grep to figure out if the thing you want to replace is there then using sed to replace it?
Below is the pattern we use with
sed -rn
orsed -r
.The entire search and replace command ("s/.../.../...") is optional. If the search and replace is used, for speed and having already matched $matchRe, we use as fast a $searchRe value as possible, using . where the character does not need to be re-verified and .{$len} for fixed length sections of the pattern.
The return value for none found is $notFoundExit.
For the following reasons:
Varying the case of Q commands will vary the behavior depending on when the exit should occur. Behaviors involving the application of Boolean logic to a multiple line input requires more complexity in the solution.
This might work for you (GNU sed):
If the
search-string
is found it will be replaced withreplacement-string
and at end-of-filesed
will exit with0
return code. If no substitution takes place the return code will be1
.A more detailed explanation:
In sed the user has two registers at his disposal: the pattern space (PS) in which the current line is loaded into (minus the linefeed) and a spare register called the hold space (HS) which is initially empty.
The general idea is to use the HS as a flag to indicate if a substitution has taken place. If the HS is still empty at the end of the file, then no changes have been made, otherwise changes have occurred.
The command
/search-string/
matchessearch-string
with whatever is in the PS and if it is found to contain thesearch-string
the commands between the following curly braces are executed.Firstly the substitution
s//replacement-string/
(sed uses the last regexp i.e. thesearch-string
, if the lefthand-side is empty, sos//replacement-string
is the same ass/search-string/replacement-string/
) and following this theh
command makes a copy of the PS and puts it in the HS.The sed command
$
is used to recognise the last line of a file and the following then occurs.First the
x
command swaps the two registers, so the HS becomes the PS and the PS becomes the HS.Then the PS is searched for any character
/./
(.
means match any character) remember the HS (now the PS) was initially empty until a substitution took place. If the condition is true thex
is again executed followed byq0
command which ends all sed processing and sets the return code to0
. Otherwise thex
command is executed and the return code is set to1
.N.B. although the
q
quits sed processing it does not prevent the PS from being reassembled by sed and printed as per normal.Another alternative:
or:
I had wanted to truncate a file by quitting when the match was found (and exclude the matching line). This is handy when a process that adds lines at the end of the file may be re-run. "Q;Q1" didn't work but simply "Q1" did, as follows:
if sed -i '/text I wanted to find/Q1' file.txt then insert blank line at end of file + new lines fi insert just the new lines without the blank line
As we already know, when sed fails to match then it simply returns its input string - no error has occurred. It is true that a difference between the input and output strings implies a match, but a match does not imply a difference in the strings; after all sed could have simply matched all of the input characters. The flaw is created in the following example
where
g=Xabc1
gives 1, while settingg=abc1
gives 2; yet both of these input strings are matched by sed! So, it can be hard to determine whether sed has matched or not. A solution:in which case the 1 is printed if-and-only-if sed has matched.
as @cnicutar commented, the return code of a command means if the command was executed successfully. has nothing to do with the logic you implemented in the codes/scripts.
so if you have:
sed will return
0
but if you write some syntax/expression errors, or the input/file doesn't exist, sed cannot execute your request, sed will return 1.workaround
this is actually not workaround. sed has
q
command: (from man page):here you can define exit-code as you want. For example
'/foo/!{q100}; {s/f/b/}'
will exit with code 100 iffoo
isn't present, and otherwise perform the substitution f->b and exit with code 0.Matched case:
Unmatched case:
I hope this answers your question.
edit
I must add that, the above example is just for one-line processing. I don't know your exact requirement. when you want to get
exit 1
. one-line unmatched or the whole file. If whole file unmatching case, you may consider awk, or even do agrep
before your text processing...