0

I want to replace windows server paths (\\100.100.0.0\aaa\bbb\ccc\) with linux paths /foo/bar/ on all occurrences inside all .txt files in a folder (/xxx/yyy/zzz/).

For that I made a loop that does not seem to work either on bash or tcsh (using foreach).

#!/bin/bash
FILES="/xxx/yyy/zzz/*.txt"
for f in $FILES
do
        sed -i 's+\\100.100.0.0\aaa\bbb\ccc\+/foo/bar/+g' $f
done

The error I get is the following:

sed: -e expression #1, char 133: unterminated `s' command

I am guessing the issue must be in how sed handles these tricky characters but I cannot identify it.

Thank you in advance.

1 Answer 1

0

\ is an escaping operator in regexp syntax (and many other things), so needs to be escaped itself, along with . which is also special there. Here, the \ in front of + caused the + not to be taken as the delimiter in s+pattern+replacement+flag.

You also don't need the loop nor bash.

#! /bin/sh -
sed -i 's|\\\\100\.100\.0\.0\\aaa\\bbb\\ccc\\|/foo/bar/|g' /xxx/yyy/zzz/*.txt

If you wanted to use bash and a variable, you'd rather use an array:

#! /bin/bash -
shopt -s nullglob
files=( /xxx/yyy/zzz/*.txt )
if (( ${#files[@]} > 0 )); then
  sed -i -- 's|\\\\100\.100\.0\.0\\aaa\\bbb\\ccc\\|/foo/bar/|g' "${files[@]}"
fi

Though if you're going to use a shell other than sh, bash is a relatively poor choice (not as poor as (t)csh of course which shouldn't be used in this century). Several shells including zsh, ksh93 or fish would make it easier and cleaner.

1
  • This works perfectly! Thank you very much, I will familiarize myself with other shells.
    – CodingPear
    Commented Jan 30, 2023 at 20:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .