I am trying to use sed to replace '0' with the word 'correct' (I am using echo $? to validate my script and I want to replace the output of echo). The problem is that I have more text in the output file and sed is replacing everything and not just "0".

For example:

some command
echo ``date` `>> "file"
echo $? >> "file"
sed -i 's/0/correct/g' /"file"

Any ideas how could I get this working?

EDIT 1 13.02.2014

Output of echo $? is "0" (what I know is that 0 means everything is OK).

Next thing is that sed -i 's/0/correct/g' /"file" (I just used "" because somebody may misinterpret it with specific name).

Here is part of the script:

echo `date` | cat >> /file.txt
echo $? | cat >> /file.txt
sed -i 's/0/correct/g' /file.txt

-i means:

   text   Insert text, which has each embedded newline preceded by a back‐slash.

From your code snippet, the sed expression looks correct - it will replace every '0' character with the characters 'correct'. If you only want the output from your echo command to be updated, you could put the sed command on the output from echo, eg:

some command
date >> /file.txt
echo $? | sed 's/0/correct/' >> /file.txt

(note removing 'echo' and 'cat' commands will have no effect on the output)

In the context you're using the -i option, it's actually a commandline option, rather than a sed script command - it's a different section in the man page:

       -i[SUFFIX], --in-place[=SUFFIX]

          edit files in place (makes backup if extension supplied)

This means it will update the source file (which is precisely what you want in your example).

However, note that in this example, the exit code being checked is the exit code of date, and not your command. In fact in your example, it's checking the exit code of echo, which I suspect is even less helpful for you.

A more standard way of performing this check would be along the lines of:

some command
date >> /file.txt
if [ $result -eq 0 ]
  echo "Command finished successfully" >> /file.txt
  echo "Command failed with exit code $result" >> /file.txt
  • As your code suggests, every 0 was replaced with word correct.| Thu 13 Feb 2014 19:13:45 GMT > Thu 13 Feb 2correct14 19:13:45 GMT – user299930 Feb 13 '14 at 19:49
  • What I want is to replace only 0 that was added with echo $? not the 0 in date and time. I want to set sed to replace only 0 and exclude 2014, 20:00 and so on. – user299930 Feb 13 '14 at 19:53

If you want to replace only the lines containing '0' and nothing else, use:

sed -i 's/^0$/correct/g' file.txt

Where ^ and $ are special characters signalling the beginning and the end of the line respectively.

A potential issue could be if there are any other rows in the file only containing '0', not just the $? return values. A way to improve this would be to print more than just $?:

echo RETVAL: $?

And then later match this string:

sed -i 's/^RETVAL: 0$/correct/g' file.txt

If you really don't need to search through the output file and make changes to it, then it would be easier to just change what you write to it in the first place.

With this bit of script, if the command is successful and returns an exit code of "0", then "correct" will be written to your file: "/file.txt". If the command returns any other exit code, that exit code (number) will be written to your file.

some command
[ $r -eq 0 ] && r="correct"
echo "`date` $r" >> /file.txt

Note: this will output the date, and your message ("correct") on the same line, which you may have wanted. If you wanted the date on one line, and your message on its own line, then change it to be two echo commands like:

some command
[ $r -eq 0 ] && r="correct"
echo `date` >> /file.txt
echo $r >> /file.txt

There are (at least) two other possibilities you may be trying to accomplish:

First, if you only want to write to the file if the command is successful (returns an exit code of "0"). This will write "correct" to your file on success, but otherwise, if any other exit code is returned, then nothing will be written to your file:

some command
[ $? -eq 0 ] && echo "`date` correct" >> /file.txt

Or, if you want to identify all exit codes where an exit code of "0" equals "correct", and any other exit code equals "error", then:

some command
if [ $r -eq 0 ]; then
    r="error: exit code $r"
echo "`date` $r" >> /file.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.