37

I have a file with the following contents:

WORD1 WORD2 WORD3

How can I use sed to replace the string between WORD1 and WORD3 with foo, such that the contents of the file are changed to the following?:

WORD1 foo WORD3

I tried the following, but obviously I'm missing something because that does not produce the desired results:

sed -i '' 's/WORD1.*WORD3/foo/g' file.txt

Camden S.
  • 2,185
  • 1
  • 22
  • 27
  • Could there be something like `WORD1 foo WORD3 bar WORD1 baz WORD3` in your string (i. e. more than one match per string)? – Tim Pietzcker May 16 '12 at 07:18
  • Ah - good question, but no, it's not really necessary to accommodate for more than one match in this scenario. – Camden S. May 16 '12 at 12:49

4 Answers4

51
sed -i 's/WORD1.*WORD3/WORD1 foo WORD3/g' file.txt

or

sed -i 's/(WORD1).*(WORD3)/\1 foo \2/g' file.txt

You might need to escape round brackets, depends on your sed variant.

A.H.
  • 63,967
  • 15
  • 92
  • 126
vyegorov
  • 21,787
  • 7
  • 59
  • 73
7

This might work for you:

sed 's/\S\+/foo/2' file

or perhaps:

sed 's/[^[:space:]][^[:space:]]*/foo/2' file

If WORD1 and WORD3 occur more than once:

echo "WORD1 WORD2 WORD3 BLA BLA WORD1 WORD4 WORD3" |
sed 's/WORD3/\n&/g;s/\(WORD1\)[^\n]*\n/\1 foo /g'
WORD1 foo WORD3 BLA BLA WORD1 foo WORD3
potong
  • 55,640
  • 6
  • 51
  • 83
  • `[^[:space:]][^[:space:]]*` can be replaced by `[^[:space:]]\+` (as you have used in your first example) in some versions of `sed`. The `/2` is a nice touch. +1 – Dennis Williamson May 17 '12 at 04:47
3

content of a sample file.txt

$ cat file.txt 
WORD1 WORD2 WORD3
WORD4 WORD5 WORD6
WORD7 WORD8 WORD9

(Correction by @DennisWilliamson in comment)
$ sed -e 's/\([^ ]\+\) \+\([^ ]\+\) \+\(.*\)/\1 foo \3/' file.txt

WORD1 foo WORD3
WORD4 foo WORD6
WORD7 foo WORD9

while awk is somehow simpler

$ awk -F' ' '{ print $1" foo "$3 }' file.txt

WORD1 foo WORD3
WORD4 foo WORD6
WORD7 foo WORD9
Community
  • 1
  • 1
Rony
  • 1,674
  • 11
  • 10
  • 1
    It's not necessary to escape any of the spaces. And `[^\ ]` means "any character other than space or backslash. I would suggest `\+` instead of `*` since, as is, your command could replace an empty line. Also `g` is unnecessary - as is the redirection. – Dennis Williamson May 17 '12 at 04:40
  • Now it won't work at all. You misunderstood what I meant. `sed -e 's/\([^ ]\+\) \+\([^ ]\+\) \+\(.*\)/\1 foo \3/' file.txt` – Dennis Williamson May 17 '12 at 11:22
0

The simplest answer to the initial question is:

Linux (GNU sed):

sed -i '/WORD1/, /WORD3/ s/WORD2/foo/' file.txt

MacOS (UNIX sed):

sed -i '' '/WORD1/, /WORD3/ s/WORD2/foo/' file.txt

  • The question is not about "replacing `WORD2`" but "replacing the string **between WORD1 and WORD3**" – Bruno Apr 04 '23 at 17:46
  • Ah yes you're right, thanks! The above will work only if you know the string, I got sidetracked by the example. – Linux Pronto Apr 05 '23 at 09:49