25
sed 's/[long1][long2]/[long3][long4]/' file.txt

I would like to split this command onto multiple lines - f.x. something like this:

sed 's/
    [long1]
    [long2]
  /
    [long3]
    [long4]
  /' file.txt

Using \ or separating strings didn't work.

4
  • 2
    stackoverflow.com/questions/8078872/…
    – Nidal
    Commented Jul 28, 2014 at 10:12
  • That's why Perl has the /x modifier to substitution.
    – choroba
    Commented Jul 28, 2014 at 10:38
  • 1
    @Networker: yes, thanks, I added my answer there, too
    – Raffael
    Commented Jul 28, 2014 at 20:57
  • Just to point out: this question is not about sed at all. It is about how to correctly break a line in a terminal command. (Note: I don't mean to say there is anything wrong with the question. However, a retagging may be well worth consideration. - Or at least adding one or two other relevant tags ...) Commented Oct 24, 2020 at 13:37

4 Answers 4

15
sed 's'/\
'[long1]'\
'[long2]'\
'/'\
'[long3]'\
'[long4]'\
'/' file.txt

Splitting on several lines with backslash does work if new lines are not indented.

$ echo "a,b" | sed 's/\(.'\
> '\),\(.\)/\2-\1/'
b-a

Tested on Cygwin with GNU sed 4.2.2

1
  • Splitting on several lines with backslash does work if new lines are not indented. That did it for me. Thanks and cheers! :-) Commented Oct 24, 2020 at 13:35
14

Another aspect:

How maintainable are the [long] items above?

The shorter and more distinct you can keep the clauses, the easier they will be to change and enhance as time passes and (sed-external) facts change around the the function they provide.

e.g.

sed \
-e 's/[minimal-s1]/[minimal-r1]/' \
-e 's/[minimal-s2]/[minimal-r2]/' \
-e 's/[minimal-s3]/[minimal-r3]/' \
-e 's/[minimal-s4]/[minimal-r4]/' 

... might be something to strive for and prefer.

1
  • I like this answer. It's useful to know though that because you're breaking up everything into their own arguments, an indent will work fine on the subsequent lines.
    – alife
    Commented Oct 26, 2021 at 14:22
9

You can save long string to bash variables, then use in sed command:

string=[long1][long2]
replace=[long3][long4]
sed -e 's/'"$string"'/'"$replace"'/' file

If you can use perl, you can break long pattern with x modifier:

perl -e 's/
[long1]
[long2]
/[long3][long4]/x' file
2
  • 3
    Why do you use so many quotes? sed "s/$string/$replace/" file.txt is fine. Commented Jul 28, 2014 at 20:50
  • @alife: Um, yeah, OK, but … why?  Does it ever make more sense to say echo 'c'"a"'t' rather than echo "cat", when you know that c, a and t are not special characters? I’m not questioning his use of some quotes, I’m saying he uses more than he needs. Can you give an example of string and replace where 's/'"$string"'/'"$replace"'/' works but "s/$string/$replace/" doesn’t? His answer will still fail if string or replace contains a slash. Commented Oct 26, 2021 at 14:37
7

You can also break that up this way:

sed '/[long1][long2]/
    s//[long3][long4]/'

Or maybe like:

sed "$(
    printf 's/[%s][%s]/[%s][%s]/' \
        long1 \
        long2 \
        long3 \
        long4
)"

Or with a heredoc:

sed -f - <<SED file.txt
s/\
[long1]\
[long2]\
/\
[long3]\
[long4]\
/
SED

You must log in to answer this question.

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