How do I exclude match and change only specific character with sed

user299930

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.
tenorkev

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
result=$?
date >> /file.txt
if [ $result -eq 0 ]
then
  echo "Command finished successfully" >> /file.txt
else
  echo "Command failed with exit code $result" >> /file.txt
fi

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get sed to only match to the first occurrence of a character?

From Dev

How do I remove first character in specific column using sed

From Dev

How do I replace only specific match in a match collection?

From Dev

How do I replace only specific match in a match collection?

From Dev

How do I have sed only perform actions on the first match?

From Dev

How do I exclude a specific record in SQL?

From Dev

Regex - Exclude a match if it is preceeded by a specific character

From Dev

Match only if no more of specific character

From Dev

How do I match multiple addresses in sed?

From Dev

How do i limit this match to one character

From Dev

How do i limit this match to one character

From Dev

How do I match a substring in either the first position or in the middle but with a specific character in front of it with a regular expression?

From Dev

How can I match specific character between specific range in the string?

From Dev

How to grep specific lines and print only those that match character

From Dev

I need to exclude the specific two-character string `[?` from a regex match search

From Dev

How can I replace the eighteenth character before space character (without match this only match the space character )?

From Dev

Using Karma, how do I exclude all files that match a pattern except for those within a specific sub-folder?

From Dev

sed - how to exclude multiple patterns in the match

From Dev

What is the ^I character and how do I find it with sed?

From Dev

How do I change the server character set?

From Dev

How do I check whether a character in a string is a specific character in JavaScript?

From Dev

How do I match only dotfiles in bash?

From Dev

Is there any way to match a string with only one character into character 'X' in sed?

From Dev

Is there any way to match a string with only one character into character 'X' in sed?

From Dev

How do I exclude a package for a specific findbugs rule

From Dev

how do i exclude specific variables from a glm in R?

From Dev

how do I exclude a specific folder on the destination when using rsync?

From Dev

how do i exclude specific variables from a glm in R?

From Dev

How do I exclude a specific extension from recursive file copying?

Related Related

  1. 1

    How can I get sed to only match to the first occurrence of a character?

  2. 2

    How do I remove first character in specific column using sed

  3. 3

    How do I replace only specific match in a match collection?

  4. 4

    How do I replace only specific match in a match collection?

  5. 5

    How do I have sed only perform actions on the first match?

  6. 6

    How do I exclude a specific record in SQL?

  7. 7

    Regex - Exclude a match if it is preceeded by a specific character

  8. 8

    Match only if no more of specific character

  9. 9

    How do I match multiple addresses in sed?

  10. 10

    How do i limit this match to one character

  11. 11

    How do i limit this match to one character

  12. 12

    How do I match a substring in either the first position or in the middle but with a specific character in front of it with a regular expression?

  13. 13

    How can I match specific character between specific range in the string?

  14. 14

    How to grep specific lines and print only those that match character

  15. 15

    I need to exclude the specific two-character string `[?` from a regex match search

  16. 16

    How can I replace the eighteenth character before space character (without match this only match the space character )?

  17. 17

    Using Karma, how do I exclude all files that match a pattern except for those within a specific sub-folder?

  18. 18

    sed - how to exclude multiple patterns in the match

  19. 19

    What is the ^I character and how do I find it with sed?

  20. 20

    How do I change the server character set?

  21. 21

    How do I check whether a character in a string is a specific character in JavaScript?

  22. 22

    How do I match only dotfiles in bash?

  23. 23

    Is there any way to match a string with only one character into character 'X' in sed?

  24. 24

    Is there any way to match a string with only one character into character 'X' in sed?

  25. 25

    How do I exclude a package for a specific findbugs rule

  26. 26

    how do i exclude specific variables from a glm in R?

  27. 27

    how do I exclude a specific folder on the destination when using rsync?

  28. 28

    how do i exclude specific variables from a glm in R?

  29. 29

    How do I exclude a specific extension from recursive file copying?

HotTag

Archive