Question

I have a file with user accounts:

[account]
username                      = mike
password                      = mike
expdate                       = 2015-02-14
city                          = blah
address                       = blah

[account]
username                      = george
password                      = george
city                          = blah
address                       = blah
expdate                       = 2015-02-14

[account]
username                      = hans
password                      = hans
city                          = blah
expdate                       = 2015-02-14
address                       = blah

So each user account starts with [account] and has certain values beneath it.

I'd like to use GNU sed to alter the expdate value for user "mike" to:

expdate                       = 2016-02-14

So it should only alter it after the first occurrence of:

username                      = mike

The problem is, expdate can be any amount of lines beneath the username.

Can this be solved with GNU sed?

Was it helpful?

Solution 3

You can try this sed,

sed '/mike/,/\[account\]/{s/\(expdate[^=]\+=\).*/\1 2016-02-14/}' yourfile

OTHER TIPS

awk '/^username/{u=$3}
     {
       if(u=="mike" && ($1~/expdate/))
       $3="2016-02-14"
     }1' your_file

This seems to be an OScam pay server ;)
Changing the config file manually of the OSCam server is not a good ting, its need a restart to read it.

This is version based on user1939168 version
Formatting is not changed, and it does not matter if its space or tab separated.

awk '/^username/{f=$3} f=="mike" && $1=="expdate" {sub(/2015/,"2016")}1'
[account]
username                      = mike
password                      = mike
expdate                       = 2016-02-14
city                          = blah
address                       = blah

This assume that user name comes before expdate and it OSCam it does, so should work fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top