Duvdevan Duvdevan - 4 days ago 5
Linux Question

How to change a line in a text file with echo?

Let's say we have a file named

profile.cfg
containing the following:

user.name=Duvdevan
user.email=duvdevan@duvdevan.duv
user.web=http://duvdevan.duv
user.country=Bosnia/Herzegovina
user.age=26
user.biography="A web developer.\n\nLikes echo command..."


Now, I'd like to update contents of line three, the
user.web
property and place it in the same position as it is currently to keep above and bellow contents as they are.

All I have tried so far is:
cat profile.cfg | grep user.web
and I get the value of that line...

Answer

You can do this easily using sed

$ sed 's#user.web=.*#user.web=http://NEW_DOMAIN#' file
user.name=Duvdevan
user.email=duvdevan@duvdevan.duv
user.web=http://NEW_DOMAIN
user.country=Bosnia/Herzegovina
user.age=26
user.biography="A web developer.\n\nLikes echo command..."

Use -i option to make inline changes to file so that no redirection is required.

Comments