Reputation: 503
I would like to change all occurrences like "waaa", "wwwaaaaa", "wa" by trimming all the "a" in the end.
waaa > w
wwa > ww
waaaaa > w
I know how to find these strings in a text using regular expressions:
grep -nE "wa+" file.txt
I also know how to change one string in a bash script
#!/bin/bash
s1="wwwwaaaaaaaa"
s2=${s1%w*}
echo "$s1 --> $s2"
(modified script from https://stackoverflow.com/a/27658717/5219155)
But I would like to use someting like
sed -E 's/wa+/ZZZ/' file.txt
where ZZZ is something that I need here.
sed -E 's/wa+/${$1%w*}/' file.txt
doesn't work.
Sample input:
Lorem ipsum wa waaaaaaa saaa dolor sit amet, consectetur aw awwwwaaaa adipiscing elit
Desirable output:
Lorem ipsum w w saaa dolor sit amet, consectetur aw awwww adipiscing elit
Upvotes: 0
Views: 493
Reputation: 7519
sed 's/waa*\b/w/g' file
sed 's/waa*\>/w/g' file
or using extended regex:
sed -r 's/wa+\b/w/g' file
\b
and \>
are word boundaries. For more information, see this regex tutorial: Word Boundaries.
Upvotes: 5
Reputation: 247220
With bash, use extended pattern matching to remove all the "a" from the end of the string:
shopt -s extglob
for str in "waaa" "wwwaaaaa" "wa"; do
new="${str%%+(a)}ZZZ"
echo "$str => $new"
done
waaa => wZZZ
wwwaaaaa => wwwZZZ
wa => wZZZ
Upvotes: 0