0

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.

Inian
  • 12,807

2 Answers2

3

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
igal
  • 9,886
0
new_port=12345
awk -v port="$new_port" '$0=="config['\'port\''] = 11111" { sub("11111",port); };
    { print; }' /path/to/file
Hauke Laging
  • 90,279