sed - linux delete multiple lines in a file -
i ios developer, not familiar linux, searched long time still not solve problem. hope can me, thanks!
before delete:
other contents...... # # proxy server iterm & terminal setup script # version 0.1 # vincentsit # nov 26, 2014 # function start_proxy { export http_proxy='abc.efgh.com:1234' export https_proxy='abc.efgh.com:1234' } function stop_proxy { export http_proxy='' export https_proxy='' } other contents...... after delete:
other contents...... other contents...... update
i'm writing shell script(can not use other languages.). on users system has text file named abc, content this:
# other comments...... # other comments...... # other comments...... other contents...... # other comments...... # # proxy server iterm & terminal setup script # version 0.1 # vincentsit # nov 26, 2014 # function start_proxy { export http_proxy='abc.efgh.com:1234' export https_proxy='abc.efgh.com:1234' } function stop_proxy { export http_proxy='' export https_proxy='' } # other comments...... other contents...... # other comments...... # other comments...... which other contents ...... fictional, can not use deleted condition.
i need delete following sections:
# # proxy server iterm & terminal setup script # version 0.1 # vincentsit # nov 26, 2014 # function start_proxy { export http_proxy='abc.efgh.com:1234' export https_proxy='abc.efgh.com:1234' } function stop_proxy { export http_proxy='' export https_proxy='' } do not need delete other contents in file.
because runs on user's system, not know line number of contents want delete.
finally this:
# other comments...... # other comments...... # other comments...... other contents...... # other comments...... # other comments...... other contents...... # other comments...... # other comments...... i think answer might this:
sed xxxxxx abc.txt
awk xxxxxx abc.txt
this robustly delete precisely asked for:
$ cat bad # # proxy server iterm & terminal setup script # version 0.1 # vincentsit # nov 26, 2014 # function start_proxy { export http_proxy='abc.efgh.com:1234' export https_proxy='abc.efgh.com:1234' } function stop_proxy { export http_proxy='' export https_proxy='' } .
$ cat file # other comments...... # other comments...... # other comments...... other contents...... # other comments...... # # proxy server iterm & terminal setup script # version 0.1 # vincentsit # nov 26, 2014 # function start_proxy { export http_proxy='abc.efgh.com:1234' export https_proxy='abc.efgh.com:1234' } function stop_proxy { export http_proxy='' export https_proxy='' } # other comments...... other contents...... # other comments...... # other comments...... .
$ awk -v rs='^$' -v ors= 'nr==fnr{bad=$0;next} s=index($0,bad){$0=substr($0,1,s-1) substr($0,s+length(bad))} 1' bad file # other comments...... # other comments...... # other comments...... other contents...... # other comments...... # other comments...... other contents...... # other comments...... # other comments...... $ it uses gnu awk multi-char rs. it's do-able in other awks if needed not going put effort until find out if solution looking for.
with bad in string:
$ awk -v rs='^$' -v ors= -v bad="# # proxy server iterm & terminal setup script # version 0.1 # vincentsit # nov 26, 2014 # function start_proxy { export http_proxy='abc.efgh.com:1234' export https_proxy='abc.efgh.com:1234' } function stop_proxy { export http_proxy='' export https_proxy='' } " 's=index($0,bad){$0=substr($0,1,s-1) substr($0,s+length(bad))} 1' file # other comments...... # other comments...... # other comments...... other contents...... # other comments...... # other comments...... other contents...... # other comments...... # other comments......
Comments
Post a Comment