0

I'm trying replace a word in a file using sed. In the same bash script I use the command :

sed -i "s/${list[$index]}/${phone}/g" $1 

And it's working flawlessly on the first function, but the second function I wrote:

sed -i "s/${list[$index]}/${zipcode}/g" $1 

Outputs this error:

sed: -e expression #1, char 0: no previous regular expression 

I'm really desperate, I'm pretty sure that it's a dumb mistake I'm doing but I can't sort it out

8
  • is something like this zipcode=4763A5 it does not contains any special char, it contains only hex chars. The same applies to phone Commented Apr 6, 2017 at 14:15
  • Nope, only hex chars there too. I find it really strange because they are the same almost a copy/paste Commented Apr 6, 2017 at 14:19
  • Try with a different sed command separator (i.e sed -i "s|${list[$index]}|${zipcode}|g" $1). Also try to remove -i switch for testing and also try to replace $1 with the real file. Switch -i will not work if $1 is not a real valid file (avoid also relative paths) Commented Apr 6, 2017 at 14:21
  • 3
    Please include the exact output of printf '%q\n' "${list[$index]}" "${phone}" "${zipcode}" in your question. Commented Apr 6, 2017 at 14:25
  • 2
    Given that error, it looks like ${list[$index]} is the empty string. Commented Apr 6, 2017 at 17:02

1 Answer 1

1

When the first half of a sed substitute command is empty:

sed 's//foo/' <<< bar 

It returns this error:

sed: -e expression #1, char 0: no previous regular expression 

Therefore, as William Pursell commented, there's a value of the ${list[@]} array that's empty, or maybe $index is out of the array's range.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.