scripting - Insert text between two marker lines in bash -
i have lines stored in text file called bashrc_snippet
. insert them .bashrc
. since change contents of text file able re-insert them in .bashrc-file. want use marker lines:
# user things histsize=1000 #start alias ls='ls --color=tty' ... more lines #end
i bash script (possibly using sed or awk). algoritm should be:
- if marker lines missing add them @ end of file (and lines of text)
- if marker lines present, replace contents between them new lines of text
don't understand requirement, here's guess
#!/bin/bash rcfile="$1" snippet="$2" var=$(<"$snippet") if grep -q "start" "$rcfile" ;then awk -v v="$var" '/start/ { print $0 print v f=1 }f &&!/end/{next}/end/{f=0}!f' "$rcfile" >t && mv t "$rcfile" else echo "#start" >> "$rcfile" echo "$var" >> "$rcfile" echo "#end" >> "$rcfile" fi
to use:
$ ./test.sh rc_file bashrc_snippet
Comments
Post a Comment