Replace specific parameter value in file using bash script

Posted on 2011-10-15
Last Modified: 2013-12-26
Hello, suppose I have a configuration file called input.dat, and it is of the following format:
height 182
mass 70
fingers 10

Open in new window

I need to replace, say, the value of 'mass' (70 in this case) with a value stored in a variable, say, $val.

How do I do this? I assume this is a one-liner for awk or sed (neither of which I am particularly familiar with).

Thank you.
Question by:InteractiveMind
    LVL 25

    Author Comment

    This almost works:
    cat input.dat | awk '//{print ($1=="mass" ? "mass ${var}" : $0)}'

    Open in new window

    Except it replaces 'mass 70' with 'mass ${var}'. So the equation becomes: how do I retrieve the value of a system variable from within awk?
    LVL 25

    Author Comment

    Okay, I have figured how to do it:
    awk '{print ($1=="temperature" ? "temperature " "'"$var"'" : $0)}'

    Open in new window

    I will leave this thread open, however, to see whether anyone can come up with a more elegant solution.
    LVL 68

    Assisted Solution



    i.e. close the quote before the variable and reopen it behind.

    Or write

    awk -v VAR=${var} ...

    and use VAR instead of ${var} inside awk. In this case leave the quotes as they are in your code.

    LVL 31

    Accepted Solution

    sed "s/\(mass [ ]*\)[0-9]*/\1$var/"
    LVL 31

    Assisted Solution

    Lets call the mass the key and 70 value then

    > key=mass
    > val=80
    > echo -e "mass\t70" | sed "s/\($key\s*\)[0-9]*/\1$val/"
    mass    80

    Additionally you could make a function like

    > map()
    > {
    >      echo $1 | sed "s/\($2\s*\)[0-9]*/\1$3/"
    > }
    > map "mass 70" mass 80
    mass 80
    LVL 25

    Author Closing Comment

    Very tidy. Thank you.
    LVL 31

    Expert Comment

    You are most welcome.  Glad to help.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now