我有 csv 文件,其中有数据,
Server,Department
testserver,Fin Tech
testserver,Fin Tech
testserver,Fin Tech
testserver,Fin Tech
testserver,Fin Tech
testserver,Fin Tech
testserver,HR
testserver,Fin Tech
testserver,Fin Tech
testserver,Fin Tech
testserver,Fin Tech
testserver,Fin Tech
testserver,HR
testserver,Fin Tech
testserver,Fin Tech
我需要通过连接所有值将它们保留在单个字段中,但它应该是唯一的。 例如:
Fin Tech/HR
但是我得到的结果是
Fin Tech/Fin Tech/Fin Tech/Fin Tech/Fin Tech/Fin Tech/HR/Fin Tech/Fin Tech/Fin Tech/Fin Tech/Fin Tech/HR/Fin Tech/Fin Tech
if [ -n "$current_Department" ] || [[ "$current_Department" == *"Department"* ]]; then
bs_name="$current_Department/$Department"
fi
使用任何 awk:
$ awk '
BEGIN { FS=OFS="," }
NR==1 { print; next }
!seen[$1,$2]++ {
depts[$1] = depts[$1] seps[$1] $2
seps[$1] = "/"
}
END {
for (srvr in depts) {
print srvr, depts[srvr]
}
}
' file
Server,Department
testserver,Fin Tech/HR
问题可能是 [[ ]] 测试中的 == 运算符正在进行模式匹配,而不是子字符串匹配。如果您想检查 current_Department 是否包含 Department,您应该使用 =~ 运算符。
这是一个例子:
departments=""
lineNumber=0
# Header Line
while IFS=',' read -r server department; do
# Skip the header
if (( lineNumber == 0 )); then
lineNumber=1
continue
fi
# Trim whitespace (example has entries with space in the end)
department=$(echo "$department" | xargs)
# Check if the department is already in the list
if [[ ! $departments =~ /$department/ ]]; then
departments+="/$department/"
fi
done < input.csv
# Remove the extra slashes
echo ${departments:1:-1} | tr -s '/'