我有这些安全标识符 ID (SID)
SID_1
S-1-2-12-347289564783285475
SID_2
S-1-2-12-312478290434839212
S-1-2-12-312748912347723432
S-1-2-12-123475184123489632
SID_3
SID_4
S-1-2-12-384721849326143123
S-1-2-12-324624783246712332
SID_5
SID_6
我需要将它们水平排列,这样我就可以将它们提供给只接受这种形式的 ID 的算法:
SID_1:S-1-2-12-347289564783285475
SID_2:S-1-2-12-312478290434839212:S-1-2-12-312748912347723432:S-1-2-12-123475184123489632
SID_3
SID_4:S-1-2-12-384721849326143123:S-1-2-12-324624783246712332
SID_5
SID_6
如何才能做到这一点?
答案1
和awk
:
awk '/^S-/{s=s":"$1;next}s{print s}{s=$0}END{print s}' file
SID_1:S-1-2-12-347289564783285475
SID_2:S-1-2-12-312478290434839212:S-1-2-12-312748912347723432:S-1-2-12-123475184123489632
SID_3
SID_4:S-1-2-12-384721849326143123:S-1-2-12-324624783246712332
SID_5
SID_6
答案2
使用sed
sed -i ':a;N;$!ba;s/\nS-/:S-/g' file
SID_1:S-1-2-12-347289564783285475
SID_2:S-1-2-12-312478290434839212:S-1-2-12-312748912347723432:S-1-2-12-123475184123489632
SID_3
SID_4:S-1-2-12-384721849326143123:S-1-2-12-324624783246712332
SID_5
SID_6