Reputation: 17
I have a file, which has some text similar to below:
#my-var="Group Name",*ItemInGroup
ThisIsTheResult/aaa/bbb/1
#my-var="Group Name",ItemInGroup
ThisIsTheResult/aaa/bbb/2
#my-var="Group Name",ItemInGroup2
ThisIsTheResult/aaa/bbb/3
#my-var="Another Group Name",*AnotherItemInGroup
ThisIsTheResult/aaa/bbb/4
#my-var="Another Group Name",AnotherItemInGroup
ThisIsTheResult/aaa/bbb/5
However, I would like to search through the document, and if it finds my-var="+++++++++++",*
(where +++++++++++
could be anything), then it moves on to the next line, and replaces aaa/bbb
with yyy/zzz
. The expected outcome, is as below:
#my-var="Group Name",*ItemInGroup
ThisIsTheResult/yyy/zzz/1
#my-var="Group Name",ItemInGroup
ThisIsTheResult/aaa/bbb/2
#my-var="Group Name",ItemInGroup2
ThisIsTheResult/aaa/bbb/3
#my-var="Another Group Name",*AnotherItemInGroup
ThisIsTheResult/yyy/zzz/4
#my-var="Another Group Name",AnotherItemInGroup
ThisIsTheResult/aaa/bbb/5
Was looking to do this in a shell script, but my regex and sed/awk experience isn't good enough... So hopefully someone know the answer easily :)
Cheers
David
Upvotes: 0
Views: 27
Reputation: 88543
With GNU sed:
sed '/my-var=".*",\*/{n;s|aaa/bbb|yyy/zzz|}' file
If a lines matches regex my-var=".*",\*
then read next line (n
) and search (s|||
) for aaa/bbb
and replace with yyy/zzz
.
Output:
#my-var="Group Name",*ItemInGroup ThisIsTheResult/yyy/zzz/1 #my-var="Group Name",ItemInGroup ThisIsTheResult/aaa/bbb/2 #my-var="Group Name",ItemInGroup2 ThisIsTheResult/aaa/bbb/3 #my-var="Another Group Name",*AnotherItemInGroup ThisIsTheResult/yyy/zzz/4 #my-var="Another Group Name",AnotherItemInGroup ThisIsTheResult/aaa/bbb/5
Please take a look at man sed
and The Stack Overflow Regular Expressions FAQ.
Upvotes: 1