2

I'm not very good with sed. It just looks complicated. However I've mnaged to learn how to replace all instances of a string with another string

sed 's/replace-this/with-this/FLAG'

However, if I want to say replace all words that start with "a" and have a third letter "p", with fruit, how would I do that?

jww
  • 97,681
  • 90
  • 411
  • 885
Many Questions
  • 125
  • 1
  • 10
  • I meant to say that I would like to replace all words that start with "a" and have a third letter "p", with the word "fruit". – Many Questions Jan 24 '16 at 02:05
  • Do you have to use `sed` ? or is a pure Bash solution is ok? – Rany Albeg Wein Jan 24 '16 at 05:01
  • If `sed` is optional and a pure bash solution is acceptable, then you might want to take a look at this: `read -ra arr <<< "$var"; printf -v var '%s' "${arr[*]//a?p*/fruit}";` with `var="apple is a fruit"`, for example. – Rany Albeg Wein Jan 24 '16 at 05:16
  • 1
    Possible duplicate of [Why does sed not replace all occurrences?](https://stackoverflow.com/q/15849119/608639) – jww Sep 07 '18 at 01:13

2 Answers2

2

This might work for you (GNU sed):

sed 's/\<a\wp\w*\>/fruit/g' file

This uses the \< opening word boundary as a marker for the start of a word beginning with a the character a, followed by another word character, followed by a p, followed by zero or more word characters, followed by a \> closing word boundary to be replaced by the word fruit globally.

potong
  • 55,640
  • 6
  • 51
  • 83
  • `sed` does not have a uniform sense of [word boundaries](http://stackoverflow.com/questions/17382272/beginning-and-end-of-words-in-sed-and-grep) unfortunately. `\<` and `\>` are GNU only – dawg Jan 24 '16 at 16:26
0

Given:

$ echo "$tgt"
app
apple
applesauce
grape
gum
apple is a fruit
text apple and pears

You can do:

$ echo "$tgt" | sed 's/a.p.*/fruit/'
fruit
fruit
fruit
grape
gum
fruit
text fruit

If you want the word only (not the remainder of the line) you can do:

$ echo "$tgt" | sed -E 's/a.p[a-zA-Z]*/fruit/'
fruit
fruit
fruit
grape
gum
fruit is a fruit
text fruit and pears
dawg
  • 98,345
  • 23
  • 131
  • 206
  • What about the line `apple is a fruit`? – pfnuesel Jan 24 '16 at 02:09
  • I assume `apple is a fruit` should become `fruit is a fruit`. – pfnuesel Jan 24 '16 at 02:13
  • As written, it will replace the remainder of the line after `a.p` so `apple is a fruit` and the rest of the line would become `fruit` – dawg Jan 24 '16 at 02:13
  • I'm sorry. I should've have clarified something. I want to replace only the word not the entire line. So as pfnuesel mentioned above, I would like to change the line "apple is a fruit" to "fruit is a fruit" and the line "appetizers etc" with "fruits etc" – Many Questions Jan 24 '16 at 02:14
  • 1
    Thanks also for clarifying that `fruit is a fruit` :p – l'L'l Jan 24 '16 at 02:17
  • `sed` does not have a uniform sense of [word boundaries](http://stackoverflow.com/questions/17382272/beginning-and-end-of-words-in-sed-and-grep). – dawg Jan 24 '16 at 02:30
  • You will be replacing "grasp" with "grfruit". – tripleee Jan 24 '16 at 16:27
  • @tripleee: Agreed, but how do you do a multi sed version of `\b` or `\<`? – dawg Jan 24 '16 at 16:31
  • If portability is a concern, maybe switch to Perl instead. `perl -pe 's/\ – tripleee Jan 24 '16 at 17:04