Viktor
Viktor

Reputation: 39

replacing a specific column value by searching for its variable using awk/sed utility

There is a conf file and its contents are as below

ipaddr = 192.168.10.1
hostanme = test
homefolder = /home/user1

how do i replace the value of homefolder with a different value using the awk utility. I want to be able to search the conf file using the keyword "homefolder" and then replace its value . I do not want to pick this line using the line number. The configuration file may get more details appended in future and hence using line number may be inappropriate

Upvotes: 0

Views: 37

Answers (1)

OznOg
OznOg

Reputation: 4722

you may try:

awk -vVALUE=some_path  -F= '/homefolder/ { print $1 " = " VALUE }' yourfile

where some_path should be the new value

EDIT: as suggested by @karakfa and @EdMorton, a better version could be:

awk -v new_path=some_path  -F' *= *' '$1=="homefolder" { print $1 " = " new_path }' yourfile

The difference is that the separator will be = surrounded by any number of spaces (thus there are no extra space on the ouput) and my version would have matched a line like mainhomefolder = /home/xyz when the test $1=="homefolder" will only match homefolder exactly.

Thank you for the comments

Upvotes: 2

Related Questions