Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using sed to find and replace in bash for loop

Tags:

bash

sed

I have a large number of words in a text file to replace.

This script is working up until the sed command where I get:

sed: 1: "*.js": invalid command code *

PS... Bash isn't one of my strong points - this doesn't need to be pretty or efficient

cd '/Users/xxxxxx/Sites/xxxxxx'
    echo `pwd`;

    for line in `cat myFile.txt`
    do
        export IFS=":"
        i=0
        list=()

        for word in $line; do
            list[$i]=$word
            i=$[i+1]
        done

        echo ${list[0]}
        echo ${list[1]}

        sed -i "s/{$list[0]}/{$list[1]}/g" *.js

    done
like image 479
James Zaghini Avatar asked Aug 30 '25 18:08

James Zaghini


2 Answers

You're running BSD sed (under OS X), therefore the -i flag requires an argument specifying what you want the suffix to be.

Also, no files match the glob *.js.

like image 131
Ignacio Vazquez-Abrams Avatar answered Sep 02 '25 09:09

Ignacio Vazquez-Abrams


This looks like a simple typo:

sed -i "s/{$list[0]}/{$list[1]}/g" *.js

Should be:

sed -i "s/${list[0]}/${list[1]}/g" *.js

(just like the echo lines above)

like image 31
Johnsyweb Avatar answered Sep 02 '25 10:09

Johnsyweb