Issue
I have a file containing following line
<datasource file name>: {"project": "<PROJECT NAME>", "db_config": "<DATASOURCE NAME>", "schedules": ["DAILY/INCREMENTAL"], "groups": {"All Users":"None","Viewer":"ViewerAccessType","DIY":"DIYAccessType"}}
I am thinking to use variables for
<PROJECT NAME> = project1
<DATASOURCE NAME> = Default
and then
I want to remove this line and add following lines same location in the file
view1: {"project": "project1", "db_config": "Default", "schedules": ["DAILY/INCREMENTAL"],"groups": {"All Users":"None"}}
view2: {"project": "project1", "db_config": "Default", "schedules": ["DAILY/INCREMENTAL"],"groups": {"All Users":"None"}}
Could anyone please suggest?
I tried with
sed -ir "s|^<datasource file name>:.*|view1: $datasource_var|" $SITE_CONFIG_PROPERTIES_FILE
but it replaces only one line how to add more lines as above?
Solution
Not sure I understand all the details but you could use sed
append (a
) and delete (d
) commands instead of substitute (s
). Example with GNU sed
:
sed '/^<datasource file name>:/{a\
view1: {"project": "project1"..."All Users":"None"}}
;a\
view2: {"project": "project1"..."All Users":"None"}}
;d;}' file.txt
Note: it seems to work (at least on your example) even with the outdated BSD sed
that comes with macOS.
You could also use awk
. Example with GNU awk
:
awk -v a='view1: {"project": "project1"..."All Users":"None"}}' \
-v b='view2: {"project": "project1"..."All Users":"None"}}' \
'/^<datasource file name>:/ {print a; print b; next} {print}' file.txt
Note: same remark as for sed
, it seems to work on your examples with the outdated BSD awk
that comes with macOS.
Answered By - Renaud Pacalet