I have a CSV file like this:
hostname1 | role1 | environment | tag,list | |
hostname2 | role2 | environment | tag,list,longer | |
hostname3 | role3 | environment | | |
I need a sed
expression that adds to the column with the tags (the 4th column) a new tag if there are no tags already, and inserts a new tag with a comma if there are existing tags. I have tried this:
sed "s/\(^$server |.*|.*|\) \(.*|.*|$\)/\1 new,\2/" testfile.csv
where server is defined externally like so:
server="hostname2"
but it doesn't work, and it doesn't deal with the comma if the tag list is empty.
How would you do this with sed?