0

In a unix script, I have the line

sed -i "/$1/s/a/b" filename

This finds lines containing the string $1 (which is given to the script as a parameter) and, in those lines, replaces "a" with "b".

I would like this to be case-insensitive with respect to $1. For example, if $1 is "foo", then I want the replacement to happen in all lines containing "foo" or "FOO" or "Foo", etc. What is the best way to do this?

(Note: The original version of this post omitted the first forward slash in the code, which is important context for understanding Kusalananda's answer.)

2 Answers 2

3

I'm assuming your original sed expression is /$1/s/a/b/ or /$1/ s/a/b/, including that initial / character, or you would have to rely on $1 containing an initial / (NOTE: The expression in the question was corrected after writing this). This sed command applies a substitution on all lines addressed by a particular regular expression given by the 1st positional parameter, $1.

Some implementations of sed, like GNU sed and the sed available on macOS, can make the regular expression address match case-insensitively by using an I (capital i) flag, in a similar way as to how flags may be used with the s/// command (e.g., s///g).

Assuming you're using a sed that supports this non-standard feature:

sed "/$1/I s/a/b/" file

This would substitute the first a with b on each line matching whatever regular expression is in $1 case-insensitively.

Testing:

% set -- foo ; sed "/$1/I s/a/b/" <<'EXAMPLE'
heredoc> FOO a
heredoc> a foo a
heredoc> Fool abba
heredoc> Moose able
heredoc> EXAMPLE
FOO b
b foo a
Fool bbba
Moose able
1
  • 1
    You are right; my code (both before and after I fixed it) included the initial slash, which I failed to copy-paste into the post. I will edit to fix this, but I hope you'll leave this informative answer up.
    – WillO
    Commented Oct 6, 2023 at 13:16
-2

Okay, got it. Is instead of s works. So the fixed code is:

sed -i "/$1/Is/a/b" filename

(This works for me in the t-shell; I have not tested it otherwise.)

4
  • oh, do you mine posting the code?
    – Cyberninja
    Commented Oct 6, 2023 at 3:15
  • @Cyberninja : sed -i "$1/Is/a/b" filename
    – WillO
    Commented Oct 6, 2023 at 4:25
  • @StephenKitt To be honest, they posted their answer before I posted mine. I did however totally misunderstand what they were writing due to the lack of code markup, and after re-reading it a few times I decided to keep my answer as it adds details and context. If anyone feels strongly about this, I can donate my text to this answer, alternatively make mine a community wiki.
    – Kusalananda
    Commented Oct 6, 2023 at 10:31
  • Ah, yes @Kusalananda, I didn’t notoce the timeline — I read “okay, got it” as confirming your answer! Your answer addresses the missing / which is important IMO, so I’d say it’s fine to leave everything as-is. Commented Oct 6, 2023 at 11:09

You must log in to answer this question.

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