0

I have files in which I am storing some credentials, I want shell script to read that file and check if current cred matches searched string, replace that at that location only.

I tried below code but it always append and does not even replace string.

cat $FILESOURCE | sed -e "s/$TEXTTOSEARCH/$TEXTTOREPLACE/g" >> $FILESOURCE

where

  • FILESOURCE = my .txt file to read
  • TEXTTOSEARCH = string to search
  • TEXTTOREPLACE = text to replace

Note: I don't want to mess up existing file, instead it just open file for reading check if searched string found, then replace it, if not found dont do anything to file.

AdminBee
  • 22,803

1 Answers1

3

Of course it appends, since you are using >>, which means append.

What do you have in $TEXTTOSEARCH? Are there letters only or any special characters? If you have special characters that have special meaning in regular expression, you have to escape them in order to work correctly. For instance . means any character. You should replace it with \. in your $TEXTTOSEARCH. Look here for more details.

There are many ways how to handle your files. I would suggest you use

sed -i.bak -e "s/$TEXTTOSEARCH/$TEXTTOREPLACE/g" $FILESOURCE

In that way sed will replace text in your file and make a backup with .bak extension. If you want it another way specify more in detail. Your question is not very clear, as you say you do not want to mess up existing file, and you want to replace text in that file?

nobody
  • 1,710
  • Yes those have any special characters example G]07N!qRsf2@q3I?rxI8 BR4DzujZlTHS@-oA}9eq lf!_lOf5hN1!:b_v%zHJ 8D9QDG$VZ?@h.J,20p6K @FG7Vf9YR2{9HWfI5yo{ IKpgAqYC:O@4=r!$aI{m +;n*T.Plq5lXj3R:&_5L – Iván Carvallo Sep 10 '20 at 08:38
  • But its very complicated to use escape characters it can be any , purpose is to simplify task, we just give source string and file will be replaced with given string at perticular location. – Iván Carvallo Sep 10 '20 at 08:40
  • Doing escaping character stuff is tedious here. Don't we have anything that just as a full string replace with given new string (having special characters in it ? ) – Iván Carvallo Sep 10 '20 at 09:30
  • If using sed, you will have to escape characters. https://unix.stackexchange.com/questions/32907/what-characters-do-i-need-to-escape-when-using-sed-in-a-sh-script https://unix.stackexchange.com/questions/486131/ask-sed-to-ignore-all-special-characters – nobody Sep 10 '20 at 09:39
  • is there any other way, let say I don't want to use SED command or escaping things – Iván Carvallo Sep 10 '20 at 10:15
  • There must be. I am not aware of a program that can do that, because they all support regular expressions. You can write a program yourself. It should be easy. Here are examples in Python https://pythonexamples.org/python-replace-string-in-file/ https://stackoverflow.com/questions/4128144/replace-string-within-file-contents – nobody Sep 10 '20 at 11:54
  • Here you have an example in Perl that ignores regular expressions https://stackoverflow.com/a/18794142/3887162 – nobody Sep 10 '20 at 11:59