Reputation: 69
I have a text formatted like following:
key [value]
key [value, value]
now I realized I forgot to put a comma between the key and the values array, which means I am trying to get this:
key, [value]
key, [value, value]
I thought, instead of regenerating the file which takes several hours, I could maybe easily do it with sed.
I tried cat small.txt | sed -n 's/{ \[}/{, \[}/g'
and some other variants (like without {} and without spaces, I get no error, but the file doesn't change
Upvotes: 2
Views: 206
Reputation: 626738
You may use
sed -E 's/([[:alnum:]])([[:space:]]+\[)/\1,\2/g' small.txt > newsmall.txt
Here, -E
enables the POSIX ERE regex engine and the command does the following:
([[:alnum:]])([[:space:]]+\[)
finds an alphanumeric and places it in Group 1, then grabs 1+ whitespaces and a [
into Group 2\1,\2
- replaces the match with Group 1, then ,
and then Group 2 contents.If you want to modify the file inplace, see sed edit file in place.
Upvotes: 1