首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据格式(对大内容表示抱歉)

数据格式(对大内容表示抱歉)
EN

Stack Overflow用户
提问于 2013-11-25 15:53:59
回答 1查看 62关注 0票数 0

我有一个文件里面有这样的内容:

代码语言:javascript
复制
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;

我需要把它格式化如下:-

代码语言:javascript
复制
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命令

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-25 17:29:03

代码语言:javascript
复制
#!/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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20197357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档