0

I'm trying to replace all line which starts with say Replace_ME. I used,

sed  -i -e "s/^Replace_ME.*$/Replace_ME=${Replaced}/" "$filename"

Though it works well with Linux but on android it's completely clearing the $filename file! for that reason I'm currently using,

sed -i "8i\Replace_ME=${Replaced}" "$filename"
sed -i "231i\Replace_ME=${Replaced}" "$filename"

which is very inefficient as you can see. Is there any alternative way of replacing line starting with keyword other than sed say awk? But at the same time compatible with android? Thanks for reading this.

Some53
  • 73
  • 5
  • 1
    How do you run sed on android? – choroba Apr 23 '21 at 09:03
  • terminal emulator, magisk module – Some53 Apr 23 '21 at 09:04
  • Can you give us the output on the android device of `file "$filename"` with $filename as one of your files ? – Zilog80 Apr 23 '21 at 09:05
  • say `Replaced=done replacing` then the output will be `Replace_ME=done replacing` in `$filename` file – Some53 Apr 23 '21 at 09:07
  • @Some53 : Could it be that the bash on Android for some reason has an internal variable `$/` defined? This would ruin your command. You could - for a test - run it with `set -x` turned on. – user1934428 Apr 23 '21 at 09:37
  • I actually tried without `$` as well eg. `sed -i -e "s/^Replace_ME.*/Replace_ME=${Replaced}/" "$filename"` but same result, all contents of `$filename` gets cleared – Some53 Apr 23 '21 at 09:57
  • I would want to start by finding out what version of `sed` and `awk` is running on android; please update the question with the output from `sed --version` and `awk --version` – markp-fuso Apr 23 '21 at 12:33
  • if you remove the `-i` flag, does `sed` generate the desired output? – markp-fuso Apr 23 '21 at 12:42
  • when you say 'all contents of $filename gets cleared` ... are you saying the file is empty, or the lines that contain the string `Replace_ME` are removed (but the rest of the lines are still intact)? – markp-fuso Apr 23 '21 at 13:23
  • Hi thanks for your reply, `sed --version` doesn't display any output it returns `sed --help` for awk it displays `awk version 20200206` – Some53 Apr 23 '21 at 13:30
  • @markp thanks for your reply, the whole file content gets cleared, no replacement done – Some53 Apr 23 '21 at 13:31
  • @markp for my first code, if I remove `-i` it produces blank screen, but for this code `sed -i "8i\Replace_ME=${Replaced}" "$filename` it produces desired output – Some53 Apr 23 '21 at 13:34
  • I'm not familiar with the Android scripting env so fwiw ... any chance of upgrading your `sed` (and `awk`) software to newer (preferably GNU) versions? – markp-fuso Apr 23 '21 at 13:45
  • 1
    Try it quotes with single quotes, just to be certain nothing is getting wildcard expanded. Clearly your output will then contain the literal text `${Replaced}`. You can build an argument by concatenating double-quoted sections and single-quoted sections if necessarily. – Gem Taylor Apr 23 '21 at 15:40
  • @ Gem thanks for your reply. Yes single quote works but as you said the variable does not expand – Some53 Apr 23 '21 at 16:32
  • @mark Updating is however not an option as I'm trying to make a general purpose module for distribution – Some53 Apr 23 '21 at 16:33
  • @mark thanks , I finally got it. `sed -i -e 's/^MODDIR.*$/MODDIR='"${MODDIR}"'/' "$MODDIR/Detach"'` The problem is the variable `MODDIR` wasn't double quoted `""` – Some53 Apr 23 '21 at 16:54
  • The thing is I'm getting this `$MODDIR` from system output so it's not quoted while passing to `sed` any suggestions how can I make the content of `$MODDIR` inside double quote before passing to sed? – Some53 Apr 23 '21 at 17:02
  • Say, `Another_Var=test/dir/test MODDIR='"'${Another_Var}'"'`, when I `echo $MODDIR` then I get `"test/dir/test"` as output. But if I pass `$MODDIR` to sed then the content gets cleared again – Some53 Apr 23 '21 at 17:27
  • Okay I finally got the solution. It was happening because my variable had `/` so sed was failing. The solution is `sed -i -e 's~^MODDIR.*$~MODDIR='"${MODDIR}"'~g' "$filename"`. Thanks everyone who tried to help me on this. Stack overflow is an amazing place – Some53 Apr 23 '21 at 18:03

1 Answers1

0

So it turned out the major issue was the variable content of $Replaced containing back slashs / eg, Replaced=/test/dir/test that's why sed was failing. To escape reserved character I needed to use #

sed  -i -e 's~^Replace_ME.*$~Replace_ME='"${Replaced}"'~g' "$filename"

So if you encounter a similar situation like me don't forget to use ~

Resources: How to pass a variable containing slashes to sed

Thanks everyone who invested their time to help me.

Some53
  • 73
  • 5