我如何才能让AWK从头开始阅读?

时间:2019-01-07 17:43:38

标签: bash shell parsing awk

我需要将所有文件解析为更好的格式,以产生用逗号分隔的列的结果,以考虑能够将内容导出为CSV文件。

这是我的输入示例;

.                                   D        0  Mon Dec 10 11:07:46 2018
..                                  D        0  Mon Feb 19 11:38:06 2018
RJ9-5                               D        0  Fri Nov 30 10:34:24 2018
WorkingOnClass                      D        0  Wed Feb 28 09:37:52 2018
ML-Test001                          D        0  Fri Dec  7 16:38:56 2018
TestML4Testing                      D        0  Wed Aug 22 08:58:42 2018
ML-NewDataSE SetCases1.xlsx         A  1415577  Wed Aug 29 14:00:16 2018
DR0001-Dum01                        D        0  Thu Aug 16 08:24:25 2018
DR0002-Dum02                        D        0  Thu Aug 16 09:04:50 2018
Readme File for Documentation And Data Description.docx      A    16136  Wed Aug 29 14:00:24 2018
ML Database Prototype               D        0  Thu Dec  6 15:11:11 2018
OneNote                             D        0  Mon Dec  3 09:39:20 2018
Data                                A        0  Mon Dec 10 11:07:46 2018

\RJ9-5
 .                                 D        0  Fri Nov 30 10:34:24 2018
 ..                                D        0  Mon Dec 10 11:07:46 2018
 KLR0151_Set023_Files_RJ9_05.xlsx  A   182462  Wed Apr  4 02:48:55 2018
 KLR0152_Set023_Files_RJ9_05.xlsx  A   525309  Wed Apr  4 02:53:57 2018

 \ML-Test001                          
 .                                   D        0  Wed Feb 28 09:37:52 2018
 ..                                  D        0  Mon Dec 10 11:07:46 2018
 WT_Conforming_Format1_1.docx        A   500914  Mon Feb 26 08:50:55 2018
 Conforming_Format_1_1.xlsx          A   130647  Mon Feb 26 08:52:33 2018
 DR0135_Dum01_text.xls               A   974848  Mon Feb 12 08:11:11 2018
 DR0139_Dum02_body.xls               A  1061888  Tue Jun 19 13:43:54 2018
 DataSet_File_mod0874953.xlsx        A   149835  Mon Feb 26 14:17:02 2018
 File Path For Dataset-2018.07.11.xlsx      A    34661  Mon Feb 12 09:27:17

这是脚本,可以在其中完成工作:

#!/bin/bash
awk -v OFS=, '
BEGIN { print "PATH, FILENAME, SIZE, TIMESTAMP" }
/[\\]/    { path=$0 }
$2 ~ /A/  {print path"\\"$1,$3,$4 " " $5 " " $6 " " $7 " "$8 }
' "$@"

但是正在忽略名称上带有空格的名称,因此我需要使用类似以下内容的名称进行验证:

awk -v FS="\t" '{print $1}'

但是我无法集成到Shell脚本中,因为Shell脚本的工作方式,所以我一直在考虑使AWK到最后开始阅读,因为结尾始终是相同的,剩下的就剩下了。

输出应该是这样的:

/RJ9-5/KLR0151_Set023_Files_RJ9_05.xlsx,182462,Wed Apr  4 02:48:55 2018
/RJ9-5/KLR0152_Set023_Files_RJ9_05.xlsx,25309,Wed Apr  4 02:53:57 2018
/ML-Test001/WT_Conforming_Format1_1.docx,500914,Mon Feb 26 08:50:55 2018
/ML-Test001/Format_1_1.xlsx,130647,Mon Feb 26 08:52:33 2018
/ML-Test001/DR0135_Dum01_text.xls,974848,Mon Feb 12 08:11:11 2018
/ML-Test001/DR0139_Dum02_body.xls,1061888,Tue Jun 19 13:43:54 2018
/ML-Test001/DataSet_File_mod0874953.xlsx,149835,Mon Feb 26 14:17:02 2018
/ML-Test001/File Path For Dataset-2018.07.11.xlsx,34661,Mon Feb 12 09:27:17 2018

2 个答案:

答案 0 :(得分:0)

使用GNU awk作为match()的第三个参数(而\s的简写形式[[:space:]]远不重要):

$ cat tst.awk
BEGIN { OFS="," }
{ gsub(/^\s+|\s+$/,"") }
sub(/^\\/,"/") { path = $0; next }
path == "" { next }
match($0,/^(.*[^ ]) +A +([^ ]+) +(.*)/,a) { print path "/" a[1], a[2], a[3] }

$ awk -f tst.awk file
/RJ9-5/KLR0151_Set023_Files_RJ9_05.xlsx,182462,Wed Apr  4 02:48:55 2018
/RJ9-5/KLR0152_Set023_Files_RJ9_05.xlsx,525309,Wed Apr  4 02:53:57 2018
/ML-Test001/WT_Conforming_Format1_1.docx,500914,Mon Feb 26 08:50:55 2018
/ML-Test001/Conforming_Format_1_1.xlsx,130647,Mon Feb 26 08:52:33 2018
/ML-Test001/DR0135_Dum01_text.xls,974848,Mon Feb 12 08:11:11 2018
/ML-Test001/DR0139_Dum02_body.xls,1061888,Tue Jun 19 13:43:54 2018
/ML-Test001/DataSet_File_mod0874953.xlsx,149835,Mon Feb 26 14:17:02 2018
/ML-Test001/File Path For Dataset-2018.07.11.xlsx,34661,Mon Feb 12 09:27:17

答案 1 :(得分:0)

尝试以下Perl解决方案:

$ perl -lane ' if(/^\s*$/) { $x=0;$y=0} if(/^\\/) {$x=1 ;($a=$_)=~s/\s*$//g;$a=~s/\\/\//g; } $y++ if $x==1 ; if($y>3) { s/^\s*//g; $_=~s/(.+?)\s+\S+\s+((\d+)\s+.+)/$1 $2/g;print "$a/$_" } ' essparaq.txt
/RJ9-5/KLR0151_Set023_Files_RJ9_05.xlsx 182462  Wed Apr  4 02:48:55 2018
/RJ9-5/KLR0152_Set023_Files_RJ9_05.xlsx 525309  Wed Apr  4 02:53:57 2018
/ML-Test001/WT_Conforming_Format1_1.docx 500914  Mon Feb 26 08:50:55 2018
/ML-Test001/Conforming_Format_1_1.xlsx 130647  Mon Feb 26 08:52:33 2018
/ML-Test001/DR0135_Dum01_text.xls 974848  Mon Feb 12 08:11:11 2018
/ML-Test001/DR0139_Dum02_body.xls 1061888  Tue Jun 19 13:43:54 2018
/ML-Test001/DataSet_File_mod0874953.xlsx 149835  Mon Feb 26 14:17:02 2018
/ML-Test001/File Path For Dataset-2018.07.11.xlsx 34661  Mon Feb 12 09:27:17

$ cat essparaq.txt
.                                   D        0  Mon Dec 10 11:07:46 2018
..                                  D        0  Mon Feb 19 11:38:06 2018
RJ9-5                               D        0  Fri Nov 30 10:34:24 2018
WorkingOnClass                      D        0  Wed Feb 28 09:37:52 2018
ML-Test001                          D        0  Fri Dec  7 16:38:56 2018
TestML4Testing                      D        0  Wed Aug 22 08:58:42 2018
ML-NewDataSE SetCases1.xlsx         A  1415577  Wed Aug 29 14:00:16 2018
DR0001-Dum01                        D        0  Thu Aug 16 08:24:25 2018
DR0002-Dum02                        D        0  Thu Aug 16 09:04:50 2018
Readme File for Documentation And Data Description.docx      A    16136  Wed Aug 29 14    :00:24 2018
ML Database Prototype               D        0  Thu Dec  6 15:11:11 2018
OneNote                             D        0  Mon Dec  3 09:39:20 2018
Data                                A        0  Mon Dec 10 11:07:46 2018

\RJ9-5
 .                                 D        0  Fri Nov 30 10:34:24 2018
 ..                                D        0  Mon Dec 10 11:07:46 2018
 KLR0151_Set023_Files_RJ9_05.xlsx  A   182462  Wed Apr  4 02:48:55 2018
 KLR0152_Set023_Files_RJ9_05.xlsx  A   525309  Wed Apr  4 02:53:57 2018

\ML-Test001
 .                                   D        0  Wed Feb 28 09:37:52 2018
 ..                                  D        0  Mon Dec 10 11:07:46 2018
 WT_Conforming_Format1_1.docx        A   500914  Mon Feb 26 08:50:55 2018
 Conforming_Format_1_1.xlsx          A   130647  Mon Feb 26 08:52:33 2018
 DR0135_Dum01_text.xls               A   974848  Mon Feb 12 08:11:11 2018
 DR0139_Dum02_body.xls               A  1061888  Tue Jun 19 13:43:54 2018
 DataSet_File_mod0874953.xlsx        A   149835  Mon Feb 26 14:17:02 2018
 File Path For Dataset-2018.07.11.xlsx      A    34661  Mon Feb 12 09:27:17