1

I have the following code and I kept getting the unterminated error. For example, $1 can be ABC-TXT-123 and I want to replace TEXT abc.123 in the file with ABC-TXT-123.

#!/bin/bash
id=$1
if [[ $id =~ .*TXT.* ]]; then
    sed -i "s/TEXT abc.*/TEXT $id/" someDataFile
fi
1
  • I just tried it quickly and got no error? You would need to provide us with the context causing the error, typically the argument that you pass it. Aug 31 '16 at 6:54
1

It worked fine with the following commands:

$ cat > test
asd asd asd asd
asd TEXT abc.123 asd
asd asd asd asd

$ var='ABC-TXT-123'
$ sed -i "s/TEXT abc.*/TEXT $var/" test

$ cat test
asd asd asd asd asd
asd TEXT ABC-TXT-123
asd asd asd asd asd

Maybe you could provide us also a snippet of the file...

0

On OSX I had to include an extra '' to make it work:

#!/bin/bash
id=$1
if [[ $id =~ .*TXT.* ]]; then
    sed -i '' 's/TEXT abc.*/TEXT '$id'/' someDataFile
fi

which means the content of someDataFile goes from 'TEXT abc.123' to 'TEXT ABC-TXT-123'. But your description asks for slightly different so you might want to:

    sed -i '' 's/TEXT abc.*/'$id'/' someDataFile

instead to replace the whole line and not put the word 'TEXT' back in...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.