Use awk to do this:
#!/bin/bash awk -F'|' ' { OFS = "|"; if ($5 != "" && $5 != "-") $5 = "server:" $5; print $0 }'
NOTE. I edited this script from the first version. This current, IMO is better.
Then you can process it with:
cat $FILENAME | sh $AWK_SCRIPTNAME
Switch -F'|' tells awk use | as a field separator. The if/else and printf if/else pretty straightforward. It prints the fields, and "server:" is added to column 5 only if it is not a "-" or "" .
Why is column 5, not column 4 ?: Because you use | at the beginning of each entry. So awk takes the “first” field ( $1 ) as an empty string, which, in her opinion, should have happened before this first | .
source share