I have a config.py
file containing
config['port'] = 11111
I want to edit this file. The tricky part is I want bash
to take input from me and replace 11111 with the input i give.
I have a config.py
file containing
config['port'] = 11111
I want to edit this file. The tricky part is I want bash
to take input from me and replace 11111 with the input i give.
How about this:
#!/bin/bash
# script.sh
# Prompt the user for input
echo "Choose a port number: "
# Read the input to a variable
read PORT
# Update the configuration file
sed -i "s/^\(config\['port'\] =\)\s\+[0-9]\+$/\1 ${PORT}/" config.py
If this is your input file:
# config.py
config['port'] = 123
And this is how you execute the command:
user@host:~$ bash script.sh
Choose a port number: 456
Then this is your updated file:
# config.py
config['port'] = 456
new_port=12345
awk -v port="$new_port" '$0=="config['\'port\''] = 11111" { sub("11111",port); };
{ print; }' /path/to/file
'Port' avar
– Ayush Mishra Dec 02 '17 at 15:05