66

I'm trying to find a line in a file and replace the next line with a specific value. I tried sed, but it seems to not like the \n. How else can this be done?

The file looks like this:

<key>ConnectionString</key>
<string>anything_could_be_here</string>

And I'd like to change it to this

<key>ConnectionString</key>
<string>changed_value</string>

Here's what I tried:

sed -i '' "s/<key>ConnectionString<\/key>\n<string><\/string>/<key>ConnectionString<\/key>\n<string>replaced_text<\/string>/g" /path/to/file

3 Answers 3

81

One way: Sample file

$ cat file
Cygwin
Unix
Linux
Solaris
AIX

Using sed, replacing the next line after the pattern 'Unix' with 'hi':

$ sed '/Unix/{n;s/.*/hi/}' file
Cygwin
Unix
hi
Solaris
AIX

For your specific question:

$ sed '/<key>ConnectionString<\/key>/{n;s/<string>.*<\/string>/<string>NEW STRING<\/string>/}' your_file
<key>ConnectionString</key>
<string>NEW STRING</string>
5
  • 1
    I get sed: 1: "/<key>ConnectionString< ...": bad flag in substitute command: '}' when running sed '/<key>ConnectionString<\/key>/{n;s/.*/<string>test<\/string>/}' $HOME/Documents/temp.rdp Sep 4, 2013 at 17:32
  • 18
    @bswinnerton Ran into the same problem on OS X and this hint helped: stackoverflow.com/a/4904510/479478. So essentially you need to break the curly braces into pieces: sed -i '' -e '/<key>ConnectionString<\/key>/ {' -e 'n; s/<string>.*<\/string>/<string>changed<\/string>/' -e '}' your_file
    – Eric Chen
    Sep 20, 2015 at 14:04
  • sed -i '' -e '/Unix/ {' -e 'n; s/.*/hihi/' -e '}' Jul 8, 2017 at 15:51
  • 2
    @EricChen this is amazing... I have no idea how (or better say why) it works... but indeed it does!
    – estani
    Aug 11, 2021 at 11:48
  • On Linux, in absence of command line utility like mac OS "plutil", this command is useful to manipulate XML based property files (e.g. Info.plist) used within iOS applications. Nov 3, 2021 at 6:36
58

This might work for you (GNU sed):

sed '/<key>ConnectionString<\/key>/!b;n;c<string>changed_value</string>' file

!b negates the previous address (regexp) and breaks out of any processing, ending the sed commands, n prints the current line and then reads the next into the pattern space, c changes the current line to the string following the command.

10
  • 4
    If there's any osx user interested, you can use gsed insted of sed to make it work (install it with macport or brew install gnu-sed)
    – txulu
    Feb 13, 2015 at 13:09
  • 2
    Can you breakdown what !b n and c do here?
    – ffledgling
    Apr 12, 2017 at 14:58
  • 9
    @ffledgling, !b negates the previous address (regexp) and breaks out of any processing, ending the sed commands, n prints the current line and then reads the next into the pattern space, c changes the current line to the string following the command.
    – potong
    Apr 12, 2017 at 21:58
  • @txulu good heavens, that's helpful. I was pulling my hair out on my mac.
    – Chris C
    Nov 19, 2018 at 20:31
  • @potong whoa thanks! this is precisely what i needed!
    – chrishuen
    Jan 25, 2019 at 15:51
6

It works. Additionaly is interested to mention that if you write,

sed '/<key>ConnectionString<\/key>/!b;n;n;c<string>changed_value</string>' file

Note the two n's, it replaces after two lines and so forth.

4
  • 6
    @Mausy5043 -i does not suppress output. That is the inline replace flag. This flag allows you to directly modify the file. Mar 23, 2018 at 16:33
  • @DanielOwsley correct so effectively output to the terminal is suppressed ;-)
    – Mausy5043
    Mar 24, 2018 at 18:08
  • 6
    That's a a very poor way to describe the effect of using -i, because "modify the original file" is a far more important effect than "suppress the output". If something is going to alter your files that's important to know (a bad sed command could cause you to lose data). Apr 9, 2018 at 18:58
  • 1
    @Mausy5043 tread lightly with sed, if you use -i to "suppress" output, you WILL ruin your year... possibly make the news. Its like saying: Use rm -rf / to instead of sleep 20 - both will pause for a while before returning the prompt... but with VERY different outcomes. 😅
    – Okezie
    Nov 26, 2019 at 16:47

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