The sed command below enables me to convert all the commas into the string and:
markers=$(echo generic,daily,nightly | sed -e "s/,/ and /g")
echo $markers will result in:
generic and daily and nightly
Now I would like to achieve the following:
If the word nightly is not present in the $markers it should add the string and not nightly to $markers string
Is that possible to achieve it with sed?
CodePudding user response:
You can use
#!/bin/bash
s='generic,daily,weekly,monthly
generic,daily,nightly,weekly,monthly'
markers=$(echo "$s" | sed -e "s/,/ and /g" -e "/nightly/!s/$/ and not nightly/")
echo "$markers"
Output:
generic and daily and weekly and monthly and not nightly
generic and daily and nightly and weekly and monthly
See the online demo. The /nightly/!s/$/ and not nightly/ does the following:
/nightly/!- skip a line if it containsnightly, elses/$/ and not nightly/- replace end of string withand not nightly.
