0

I can `sed -i 's/a/ONE/g' file.xml with no problems, but attempting to run through with a script and pass array variables wipes the entire file.

declare -a arr=("a" "b")
declare -a ray=("ONE" "TWO")

for i in "${!arr[@]}"
do
    sed -n -i 's/${arr[$i]}/${ray[$i]}/g' $file
done

Expected Output:

I should be replacing all instances of a & b, with ONE & TWO respectively.

Like I said I was able to complete this from the command line with

sed -i 's/a/ONE/g' file.xml

What gives?

EDIT:

Attempted a suggested fix using double quotes in the sed command, no luck.

user7823016
  • 157
  • 2
  • 11
  • Check out this question: [Shell variables in sed script](https://stackoverflow.com/questions/7006910/shell-variables-in-sed-script) Use double quotes instead of single quotes around your sed script. – JNevill Mar 18 '22 at 15:50
  • @JNevill unfortunately no, still wipes entire file when it runs that sed. – user7823016 Mar 18 '22 at 15:53
  • 1
    two issues ... 1) JNevill has addressed re: using `bash` variables in a `sed` script ... 2) the `-n` suppresses normal output (to stdout) which means `-i` has nothing to write to the file (solution: remove the `-n`) – markp-fuso Mar 18 '22 at 17:19
  • As a further comment, repeatedly running `sed -i` in a loop is also an antipattern. Instead, figure out how to combine the commands you want to run into a single script. See also https://stackoverflow.com/questions/7657647/combining-two-sed-commands – tripleee Mar 21 '22 at 05:54

1 Answers1

0

I was able to figure out what the issue is, but not completely sure why the issue was.

The -n "Suppress Automatic Printing of Pattern Space" was for some reason sabotaging the command. If anyone has insight as to why it'd be greatly appreciated!

user7823016
  • 157
  • 2
  • 11
  • 1
    when using the `-n` flag (suppress automatic printing) you then have to explicitly tell `sed` when to print something, eg: using the `p` command: `sed 's/.../.../p` – markp-fuso Mar 18 '22 at 16:10
  • @markp-fuso I didn't think my issue was printing-related. I didn't care whether anything printed to the console, I just wanted strings overwritten in my file and `-n` was preventing that? – user7823016 Mar 18 '22 at 16:34
  • `-i` says to write stdout to the file; `-n` says to send nothing to stdout; if the intention is to maintain all data in the file (plus the desired edits/replacements) then just remove the `-n` – markp-fuso Mar 18 '22 at 16:56
  • @markp-fuso ah, so the `-n` directly nullifies `-i`. – user7823016 Mar 18 '22 at 17:15
  • without any other actions/directives (eg, `p`rint), effectively, yes – markp-fuso Mar 18 '22 at 17:16
  • 1
    @user7823016 `-n` has nothing to do with `-i`. Put it this way: `sed 's/a/ONE/g' file.xml` (note: no `-i` *or* `-n`) would output the file with all "a"s replaced by "ONE". If you add `-i`, it replaces the original file with that modified version. If you remove the `-i` and instead add `-n`, it outputs *nothing*, because it's been told not to print the modified versions of each line. If you put in both `-n` and `-i`, it replaces the original file with that nothing -- the `-i` redirects the nothing from the terminal to the file. – Gordon Davisson Mar 18 '22 at 17:52
  • @GordonDavisson this makes a lot more sense. I wish I could give you the Problem Solved check. – user7823016 Mar 18 '22 at 17:53