Reputation: 3
I think i have a sed or awk question. I am trying to manipulate a config file from a bash script, but i can't find the right way to do it.
I have the following in my config file:
bla.update(
{'test': {'tag': 'twenty',
and want to replace it to:
bla.update({
'test': {'tag': 'twenty',
I tried all sort of sed & awk tutorials, but i can't find it.
It must be not too hard i guess, but i am stuck, please help me!!
Upvotes: 0
Views: 179
Reputation: 204184
With GNU awk so you can read the whole file at one time using -v RS='\0'
(will also work with SOME other awks):
$ awk -v RS='\0' -v ORS= '{gsub(/\n{/,"{\n")}1' file
bla.update({
'test': {'tag': 'twenty',
With other awks you can use some other character that's not present in the input file, e.g. a control-C or similar, instead of \0
as the Record Separator above or you can do:
$ awk -v RS= -v ORS='\n\n' '{gsub(/\n{/,"{\n")}1' file
bla.update({
'test': {'tag': 'twenty',
but it will compress sequences of consecutive blank lines to a single blank line and add a trailing blank line at the end of the file if one wasn't already present.
Finally, you can construct a string from the whole file contents read one line at a time using any awk and then operate on that string in the END section:
$ awk '{s=s $0 ORS} END{gsub(/\n{/,"{\n",s); printf "%s",s}' file
bla.update({
'test': {'tag': 'twenty',
Upvotes: 2
Reputation: 11713
Here's an attempt using sed
sed 'N;s/(\s*\n\s*{/({\n/g' file
Sample Input:
bla.update(
{'test': {'tag': 'twenty',
Output:
bla.update({
'test': {'tag': 'twenty',
Upvotes: 1
Reputation: 41460
Some like this:
awk 'f {sub(/{/,x);f=0} /bla.update/ {$0=$0 "{";f=1}1'
bla.update({
'test': {'tag': 'twenty',
It search for bla.update
and then add {
at the end and set flag f=1
If flag f
is set then remove {
from start of line and clear flag.
Upvotes: 1