Issue
I want to do the following to all of the statements in the file:
Input: xblahxxblahxxblahblahx
Output: <blah><blah><blahblah>
So far I am thinking of using sed -i 's/x/</g' something.ucli
Solution
You can use
sed 's/x\([^x]*\)x/<\1>/g'
Details:
x
- anx
\([^x]*\)
- Group 1 (\1
refers to this group value from the replacement pattern): zero or more (*
) chars other thanx
([^x]
)x
- anx
See the online demo:
#!/bin/bash
s='xblahxxblahxxblahblahx'
sed 's/x\([^x]*\)x/<\1>/g' <<< "$s"
# => <blah><blah><blahblah>
If x
is a multichar string, e.g.xyz
, it will be easier with perl:
perl -pe 's/xyz(.*?)xyz/<$1>/g'
See this online demo.
Answered By - Wiktor Stribiżew Answer Checked By - Mary Flores (WPSolving Volunteer)