我有一个包含以下内容的文件:
Hostname: Solaris10;
Modified: /software, /nsr, /var, /home;
Hostname: Win2k3sp1;
Modified: "MSSQL$SQLEXPRESS:", "MSSQL$VIM_SQLEXP:";
Hostname: WinXP;
Modified: "D:\\1", "C:\\Inetpub", "C:\\Config.Msi",
"C:\\Documents and Settings";
Hostname: Win2k3sp1;
Modified: "C:\\Program Files\\Internet Explorer",
"C:\\Program Files\\Common Files";
Hostname: Centos5;
Modified: /home, /etc, /dev, /boot;
Hostname: WinXP;
Modified: "D:\\Program Files (x86) (x86) (x86)",
"D:\\Program Files (x86)",
"D:\\98fdac7656d6632b28";
Hostname: Centos5;
Modified: /opt, /sys, /usr,
/root;
我需要像这样格式化: -
Centos5,/home
Centos5,/etc
Centos5,/dev
Centos5,/boot
Centos5,/opt
Centos5,/sys
Centos5,/usr
Centos5,/root
Solaris10,/software
Solaris10,/nsr
Solaris10,/var
Solaris10,/home
Win2k3sp1,MSSQL$SQLEXPRESS:
Win2k3sp1,MSSQL$VIM_SQLEXP:
Win2k3sp1,C:\\Program Files\\Internet Explorer
Win2k3sp1,C:\\Program Files\\Common Files
WinXP,D:\\Program Files (x86) (x86) (x86)
WinXP,D:\\Program Files (x86)
WinXP,D:\\98fdac7656d6632b28
WinXP,D:\\1
WinXP,C:\\Inetpub
WinXP,C:\\Config.Msi
WinXP,C:\\Documents and Settings
我可以使用数组,文本修饰符......但程序应该尽可能短/高效。
我已经实现了它的使用,而且很多sed替换,我不喜欢它:(!
任何帮助将不胜感激。我只能在Solaris10 / RHEL5上使用'bash'命令
答案 0 :(得分:2)
#!/bin/bash
# newlines and quotes should be ignored,
# so we delete them before we begin
# ';' is a record seperator, we translate this to \n
tr -d '\n\"' | tr '\;' '\n' |\
while IFS=':' read LINE REST
do
case $LINE in
"Hostname" ) LABEL="$REST" ;;
"Modified" ) IFS=','
for WORD in $REST
do
echo "$LABEL,${WORD//\\/\\\\}"
done
;;
esac
done |sort| sed 's/, */,/g'
# sort result and delete superfluous spaces