bash - Substitute the remaining part of line after pattern matching with input from user -


i looking bash command matches pattern in file , substitutes remaining part of line input script asks user.is possible in bash script?

for example :i have file input file program.i wish change parts of using bash script.

karyotype ='red.txt' chromosomes_units = 1000000   <ideogram>  <spacing> default = 0.001r </spacing>  radius    = 0.9r thickness = 20p fill      = yes 

so,is possible can search 'karyotype = ' using grep/sed command , change remaining part of line based on user prompt.for example,i wish change red.txt file name provided user.

you can use sed this:

# user input string str='foo.bar'  # use sed replacement sed -i.bak "s~^\( *karyotype *= *\).*$~\1'$str'~" input  # check results cat input karyotype ='foo.bar' chromosomes_units = 1000000   <ideogram>  <spacing> default = 0.001r </spacing>  radius    = 0.9r thickness = 20p fill      = yes 

alternatively can use safer awk when don't know value of str beforehand:

awk -v str="'a&b'" 'begin{fs=ofs="="} $1=="karyotype "{$2=str} 1' file 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -