我有一个文件里面有这样的内容:
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 10/RHEL5 5上使用bash命令
发布于 2013-11-25 09:29:03
#!/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
https://stackoverflow.com/questions/20197357
复制相似问题