Mike Q
Mike Q

Reputation: 7327

Using sed to add a string if missing or replace string with wrong value

How in bash with sed can I check if line contains use_authtok.*sha512 and then set or add rounds=5000 ? So far I have only figured out how to add if missing using

sed 's/sha512/sha512 rounds=5000/g' 

in a non-robust way.

example:

use_authtok try_first_pass sha512 remember=5
use_authtok try_first_pass sha512 remember=5 rounds=7000

desired output:

use_authtok try_first_pass sha512 remember=5 rounds=5000     # was missing here
use_authtok try_first_pass sha512 remember=5 rounds=5000     # changed from 7000

Upvotes: 0

Views: 319

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133428

Could you please try following awk too.

awk '
/use_authtok.*sha512/ && !sub(/rounds=[0-9]+/,"rounds=5000"){
  $0=$0 " rounds=5000"
}
1
' Input_file

Upvotes: 2

Gilles Quénot
Gilles Quénot

Reputation: 184955

Like this using :

$ awk '(/use_authtok.*sha512/){$5="rounds=5000"}1' file

 Output:

use_authtok try_first_pass sha512 remember=5 rounds=5000
use_authtok try_first_pass sha512 remember=5 rounds=5000

Upvotes: 2

Related Questions