How can I replace the last occurrence of " before ] (text-manipulation)
0
votes
5
answers
137
views
I'm doing some text manipulation and I want to replace the last occurrence of
"
(with ", "four"
) before ]
which could be in different lines. (this essentially adds a new item in the array)
e.g:
array = ["one", "two", "three"]
other_array = ["one", "two", "three"]
array = ["one", "two",
"three"]
array =
[
"one",
"two",
"three"
]
array = ["one", "two",
"three"
]
should become
array = ["one", "two", "three", "four"]
other_array = ["one", "two", "three"]
array = ["one", "two",
"three", "four"]
array =
[
"one",
"two",
"three", "four"
]
array = ["one", "two",
"three", "four"
]
P.S: -We are guaranteed to get a valid array so array = ["one", "two]", "three"]
is invalid as strings inside the array follow this pattern [a-zA-Z0-9] so you are guaranteed to get that
-Also note that they are other arrays but the one i'm interested in is an array named "array" which is unique.
Tried sed
#this works for the first scenario:
sed 's/\(array.*"\)\([^"]*]\)/\1, "newItem"\2/' file.txt
# but couldn't figure out how to do this for scenarios #2 & #3
Asked by kyles
(33 rep)
May 6, 2024, 09:29 AM
Last activity: May 7, 2024, 05:08 PM
Last activity: May 7, 2024, 05:08 PM