AWK SED操作文本以将其排序为CSV

时间:2014-10-31 22:15:04

标签: csv text awk sed

我有这个输入文件

SHARED FOLDER: \\SERVERC1\"uh4" 

Share name        \\SERVERC1\uh4
Path              C:\vol\CN_FS4\CN_QT01\FILESERVER43\userhome
Remark            
Maximum users     No limit
Users             0
Permissions:
WINDOM\NASCIFSMIG01:  FULL CONTROL
WINDOM\ADMINDA14b  :  FULL CONTROL
WINDOM\ADMINDA16   :  FULL CONTROL
The command completed successfully.
============================================================================================================  
SHARED FOLDER: \\SERVERC1\"uh5" 

Share name        \\SERVERC1\uh5
Path              C:\vol\CN_FS2\CN_QT01\FILESERVER01\userhome
Remark            
Maximum users     No limit
Users             0
Permissions:
WINDOM\NASCIFSMIG01:  FULL CONTROL
WINDOM\ADMINDA14b  :  FULL CONTROL
WINDOM\ADMINDA20   :  FULL CONTROL
The command completed successfully.
============================================================================================================  

我需要这个输出:

\\SERVERC1\uh4;C:\vol\CN_FS4\CN_QT01\FILESERVER43\userhome;WINDOM\NASCIFSMIG01:  FULL CONTROL;WINDOM\ADMINDA14b  :  FULL CONTROL;WINDOM\ADMINDA16   :  FULL CONTROL
\\SERVERC1\uh5;C:\vol\CN_FS2\CN_QT01\FILESERVER01\userhome;WINDOM\NASCIFSMIG01:  FULL CONTROL;WINDOM\ADMINDA14b  :  FULL CONTROL;WINDOM\ADMINDA20   :  FULL CONTROL

你有什么建议吗? 我不知道该怎么做。我试着这样做: sed -n“/ Permissions:/,/命令已成功完成/ p”仅获取权限但不起作用。

3 个答案:

答案 0 :(得分:2)

通过grep和paste,

$ grep -oP '^(Share name|Path)\s+\K.*|^WINDOM\\.*' file | paste -d';' - - - - -
\\SERVERC1\uh4;C:\vol\CN_FS4\CN_QT01\FILESERVER43\userhome;WINDOM\NASCIFSMIG01:  FULL CONTROL;WINDOM\ADMINDA14b  :  FULL CONTROL;WINDOM\ADMINDA16   :  FULL CONTROL
\\SERVERC1\uh5;C:\vol\CN_FS2\CN_QT01\FILESERVER01\userhome;WINDOM\NASCIFSMIG01:  FULL CONTROL;WINDOM\ADMINDA14b  :  FULL CONTROL;WINDOM\ADMINDA20   :  FULL CONTROL

答案 1 :(得分:1)

sed -nE 's/^Share name *(\\\\.*)/\1;/p;s/^Path *(.*)$/\1;/p;s/^WINDOM.*/&;/p' input.txt | tr -d "\n" | sed 's/;\\\\/;\n\\\\/g' | sed 's/;$//'

输出:

\\SERVERC1\uh4;C:\vol\CN_FS4\CN_QT01\FILESERVER43\userhome;WINDOM\NASCIFSMIG01:  FULL CONTROL;WINDOM\ADMINDA14b  :  FULL CONTROL;WINDOM\ADMINDA16   :  FULL CONTROL
\\SERVERC1\uh5;C:\vol\CN_FS2\CN_QT01\FILESERVER01\userhome;WINDOM\NASCIFSMIG01:  FULL CONTROL;WINDOM\ADMINDA14b  :  FULL CONTROL;WINDOM\ADMINDA20   :  FULL CONTROL

答案 2 :(得分:1)

这是一个awk版本,全部在一个命令中完成。

awk '{i++} i==3 {d=$3} i==4 {d=d";"$2} i>8 && i<12 {d=d";"$0} /===/{print d;i=d=""}' file

\\SERVERC1\uh4;C:\vol\CN_FS4\CN_QT01\FILESERVER43\userhome;WINDOM\NASCIFSMIG01:  FULL CONTROL;WINDOM\ADMINDA14b  :  FULL CONTROL;WINDOM\ADMINDA16   :  FULL CONTROL
\\SERVERC1\uh5;C:\vol\CN_FS2\CN_QT01\FILESERVER01\userhome;WINDOM\NASCIFSMIG01:  FULL CONTROL;WINDOM\ADMINDA14b  :  FULL CONTROL;WINDOM\ADMINDA20   :  FULL CONTROL

缩短一些:

awk '++i==3{d=$3} i==4{d=d";"$2} i>8&&i<12{d=d";"$0} /===/{print d;i=d=""}' file