0

I have this situation:

here is some file.conf :

1stRecord value
2ndRecord value
3rdRecord value

I need to replace value for some record. Here is my script:

    correct_1stRecord='new1stValue'
    correct_2ndRecord='new2ndValue'
    correct_3rdRecord='new3rdValue'
    function correct_record() {
     sed -i 's/^$1 $(cat file.conf | grep -e "^$1" | awk '{print $2}')/$1 $correct_$1/" file.conf
    }
correct_record 3rdRecord

when I run it the $correct_$1 did not switch to $correct_3rdRecord and as a result I have this record:

3rdRecord 3rdRecord 

When I expect it to be:

3rdRecord new3rdValue

I was trying to modify the second part of the sed expression to /$1 $(correct_$1) byt then it was showing that command correct_3rdRecord does not exist, however (1) it is not a command but variable, (2) I did declare it above.

eswues
  • 119
  • 1
  • 10
  • 2
    Variables aren't expanded inside single quotes, but what are you really trying to do? There's almost never going to be a reason to you use `cat`, `sed`, `grep`, and `awk` all at the same time. I think your quoting got a little mixed up when creating the question. If you're trying to use `$correct_$1` to be the name of a variable you want to expand, you should check out https://stackoverflow.com/questions/1694196/lookup-shell-variables-by-name-indirectly – Eric Renouf Jul 19 '17 at 12:00

1 Answers1

0

thanks a lot for the sugestion. I did this this way:

function go() {
  correct_userlogin='niza'
  correct_useraddress='theCenter'
  old_value=$(cat dwa | grep -e "^$1"| awk '{print $2}')
  eval $(echo correct_var='$'"correct_$1")
  echo $correct_var
  sed -i "s/^$1 $old_value/$1 $correct_var/" dwa
}
go userlogin
go useraddress

As a result I got: The records befoure:

userlogin johny
useraddress out

The records after:

userlogin niza
useraddress theCenter

works pretty well now. thanks a lot

eswues
  • 119
  • 1
  • 10