0

I have a string "st xxx street st st" and i want to change to "street xxx street street street". I can replace middle st, but how can i replace others. Here is my code so far:

#!/bin/bash

SPACE=" "
input="st xxx street st st"
search_string="st"
replace_string="street"
output=${input//$SPACE$search_string$SPACE/$SPACE$replace_string$SPACE}
4
  • 1
    Why don't you use sed/awk etc.? With bash sth like while [[ $input =~ (^|.* )st( .*|$) ]]; do input="${BASH_REMATCH[1]}street${BASH_REMATCH[2]}"; done; echo "$input" would work but it's too much work Commented Feb 27, 2020 at 12:33
  • 1
    it will work but its expensive as i have too many records. Commented Feb 27, 2020 at 12:42
  • @UmarAmin, Then as already suggested by oguz ismail try using sed or awk solutions, I think sed one is already posted. Commented Feb 27, 2020 at 12:43
  • 1
    @RavinderSingh13 thanks fam, but a solution with sed/awk is what OP exactly needs and I don't want to distract from that Commented Feb 27, 2020 at 13:03

4 Answers 4

3

Try using sed if that is possible:

echo "$input" | sed 's/\bst\b/street/g'

\b in GNU sed refers to word boundaries.

Also refer: How can I find and replace only if a match forms a whole word?

Sign up to request clarification or add additional context in comments.

Comments

2

I'm assuming that there are never any occurrences of street at line beginning or end.

I suggest you try this:

  1. Make all street occurrences to st:

    output=${input//$SPACE$replace_string$SPACE/$SPACE$search_string$SPACE}
    
  2. Now you can safely change st to street without the spaces:

    output2=${output//$search_string/$replace_string}
    

Comments

0

I have tried a 'simple-stupid' procedure, in which I first change all spaces to newlines, then search for (temporary) lines containing just 'st', which I change to street, and I revert the newlines back in the end.

input='anst xxx street st st'
echo "$input" | sed 's/ /\n/g' | sed 's/^st$/street/g' | tr '\n' ' ' | sed 's/ $/\n/'

output:

anst xxx street street street

Words only containing st shall not be substitued.

Comments

0

Add this two

output=${output//$search_string$SPACE/$replace_string$SPACE}
output=${output//$SPACE$search_string/$SPACE$replace_string}

Update on comment, then with help of |

output="|${input//$SPACE$search_string$SPACE/$SPACE$replace_string$SPACE}|"
output=${output//"|$search_string$SPACE"/$replace_string$SPACE}
output=${output//"$SPACE$search_string|"/$SPACE$replace_string}
output=${output//|}

Or like Markante Makrele suggested

output=${input//$replace_string/$search_string}
output=${output//$search_string/$replace_string}

But better use sed.

1 Comment

second one will not work because " street" will be replaced with "streetreet"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.