使用awk在匹配字符串之间打印行

时间:2018-10-29 09:25:41

标签: awk

我有一个包含以下条目的文件;

更改为:/ Out / AP / SD

sftp> ls -lrt

-rw-rw-rw-   1 user     group           0 Oct 25 10:24 HOLD_201810261247_M.csv

-rw-rw-rw-   1 user     group           0 Oct 25 10:24 HOLD_201810261247_S.csv

-rw-rw-rw-   1 user     group    1724355981 Oct 25 10:24 HOLD_201810261310.csv

-rw-rw-rw-   1 user     group    2319514056 Oct 25 10:26 FINAL_201810261347.csv

-rw-rw-rw-   1 user     group           0 Oct 25 10:26 SUMMARY_201810261343.csv

-rw-rw-rw-   1 user     group           0 Oct 26 10:16 HOLD_201810271245_S.csv

-rw-rw-rw-   1 user     group           0 Oct 26 10:16 HOLD_201810271246_M.csv

-rw-rw-rw-   1 user     group    1725252957 Oct 26 10:17 HOLD_201810271302.csv

-rw-rw-rw-   1 user     group    2244889790 Oct 26 10:21 FINAL_201810271346.csv

-rw-rw-rw-   1 user     group           0 Oct 26 10:21 SUMMARY_201810271342.csv


sftp> bye

更改为:/ Out / AS / SD

sftp> ls -lrt

-rw-rw-rw-   1 user     group    174172077 Oct 25 13:01 HOLD_201810261753.csv

-rw-rw-rw-   1 user     group    191231356 Oct 25 13:01 HOLD_201810261753_M.csv

-rw-rw-rw-   1 user     group    177010167 Oct 25 13:01 HOLD_201810261753_S.csv

-rw-rw-rw-   1 user     group    171490539 Oct 25 13:02 FINAL_201810261808.csv

-rw-rw-rw-   1 user     group           0 Oct 25 13:02 SUMMARY_201810261808.csv

-rw-rw-rw-   1 user     group    97238298 Oct 25 13:02 VAS_HOLD_201810261751.csv

sftp> bye

Changing to: /Out/BR/SD

sftp> ls -lrt

-rw-rw-rw-   1 user     group           0 Oct 25 11:24 HOLD_201810261529_S.csv

-rw-rw-rw-   1 user     group    1721060436 Oct 25 11:25 HOLD_201810261544_M.csv

-rw-rw-rw-   1 user     group    1537619643 Oct 25 11:26 HOLD_201810261546.csv

-rw-rw-rw-   1 user     group    1545973081 Oct 25 11:28 FINAL_201810261601.csv

-rw-rw-rw-   1 user     group           0 Oct 25 11:28 SUMMARY_201810261559.csv

sftp> bye

我想打印sftp> ls -lrt和sftp>再见之间的所有行,我可以使用 awk'/ sftp> ls -lrt / {flag = 1; next} / sftp> bye / {flag = 0} flag'文件名

但我想将数据打印为

/Out/AP/SD   -rw-rw-rw- 1 user group 0 Oct 25 10:24 HOLD_201810261247_M.csv
/Out/AP/SD   -rw-rw-rw- 1 user group 0 Oct 25 10:24 HOLD_201810261247_S.csv
/Out/AP/SD   -rw-rw-rw- 1 user group 1724355981 Oct 25 10:24 HOLD_201810261310.csv
.........
/Out/AS/SD  -rw-rw-rw- 1 user group 174172077 Oct 25 13:01 HOLD_201810261753.csv
/Out/AS/SD  -rw-rw-rw- 1 user group 191231356 Oct 25 13:01 HOLD_201810261753_M.csv

以此类推...

感谢您的支持...

1 个答案:

答案 0 :(得分:1)

这里是多行输出,不包括没有字符的行:

$ awk '/^Changing to: / { match($0,/^Changing to: /); dir=substr($0,RLENGTH+1); } /^sftp> ls -lrt$/ { doprint=1; next; } /^sftp> bye$/ { doprint=0; } /.+/ { if ( doprint == 1 ) print(dir " " $0); }' sftp.out
/Out/AP/SD -rw-rw-rw- 1 user group 0 Oct 25 10:24 HOLD_201810261247_M.csv
/Out/AP/SD -rw-rw-rw- 1 user group 0 Oct 25 10:24 HOLD_201810261247_S.csv
/Out/AP/SD -rw-rw-rw- 1 user group 1724355981 Oct 25 10:24 HOLD_201810261310.csv
/Out/AP/SD -rw-rw-rw- 1 user group 2319514056 Oct 25 10:26 FINAL_201810261347.csv
/Out/AP/SD -rw-rw-rw- 1 user group 0 Oct 25 10:26 SUMMARY_201810261343.csv
/Out/AP/SD -rw-rw-rw- 1 user group 0 Oct 26 10:16 HOLD_201810271245_S.csv
/Out/AP/SD -rw-rw-rw- 1 user group 0 Oct 26 10:16 HOLD_201810271246_M.csv
/Out/AP/SD -rw-rw-rw- 1 user group 1725252957 Oct 26 10:17 HOLD_201810271302.csv
/Out/AP/SD -rw-rw-rw- 1 user group 2244889790 Oct 26 10:21 FINAL_201810271346.csv
/Out/AP/SD -rw-rw-rw- 1 user group 0 Oct 26 10:21 SUMMARY_201810271342.csv
/Out/AS/SD -rw-rw-rw- 1 user group 174172077 Oct 25 13:01 HOLD_201810261753.csv
/Out/AS/SD -rw-rw-rw- 1 user group 191231356 Oct 25 13:01 HOLD_201810261753_M.csv
/Out/AS/SD -rw-rw-rw- 1 user group 177010167 Oct 25 13:01 HOLD_201810261753_S.csv
/Out/AS/SD -rw-rw-rw- 1 user group 171490539 Oct 25 13:02 FINAL_201810261808.csv
/Out/AS/SD -rw-rw-rw- 1 user group 0 Oct 25 13:02 SUMMARY_201810261808.csv
/Out/AS/SD -rw-rw-rw- 1 user group 97238298 Oct 25 13:02 VAS_HOLD_201810261751.csv
/Out/BR/SD -rw-rw-rw- 1 user group 0 Oct 25 11:24 HOLD_201810261529_S.csv
/Out/BR/SD -rw-rw-rw- 1 user group 1721060436 Oct 25 11:25 HOLD_201810261544_M.csv
/Out/BR/SD -rw-rw-rw- 1 user group 1537619643 Oct 25 11:26 HOLD_201810261546.csv
/Out/BR/SD -rw-rw-rw- 1 user group 1545973081 Oct 25 11:28 FINAL_201810261601.csv
/Out/BR/SD -rw-rw-rw- 1 user group 0 Oct 25 11:28 SUMMARY_201810261559.csv

如果您需要像原始问题中那样作为一行输出:

$ awk '/^Changing to: / { match($0,/^Changing to: /); printf("%s ",substr($0,RLENGTH+1)); } /^sftp> ls -lrt$/ { doprint=1; next; } /^sftp> bye$/ { doprint=0; } /.+/ { if (doprint == 1) printf("%s ",$0); } END { print(""); }' sftp.out
/Out/AP/SD -rw-rw-rw- 1 user group 0 Oct 25 10:24 HOLD_201810261247_M.csv -rw-rw-rw- 1 user group 0 Oct 25 10:24 HOLD_201810261247_S.csv -rw-rw-rw- 1 user group 1724355981 Oct 25 10:24 HOLD_201810261310.csv -rw-rw-rw- 1 user group 2319514056 Oct 25 10:26 FINAL_201810261347.csv -rw-rw-rw- 1 user group 0 Oct 25 10:26 SUMMARY_201810261343.csv -rw-rw-rw- 1 user group 0 Oct 26 10:16 HOLD_201810271245_S.csv -rw-rw-rw- 1 user group 0 Oct 26 10:16 HOLD_201810271246_M.csv -rw-rw-rw- 1 user group 1725252957 Oct 26 10:17 HOLD_201810271302.csv -rw-rw-rw- 1 user group 2244889790 Oct 26 10:21 FINAL_201810271346.csv -rw-rw-rw- 1 user group 0 Oct 26 10:21 SUMMARY_201810271342.csv /Out/AS/SD -rw-rw-rw- 1 user group 174172077 Oct 25 13:01 HOLD_201810261753.csv -rw-rw-rw- 1 user group 191231356 Oct 25 13:01 HOLD_201810261753_M.csv -rw-rw-rw- 1 user group 177010167 Oct 25 13:01 HOLD_201810261753_S.csv -rw-rw-rw- 1 user group 171490539 Oct 25 13:02 FINAL_201810261808.csv -rw-rw-rw- 1 user group 0 Oct 25 13:02 SUMMARY_201810261808.csv -rw-rw-rw- 1 user group 97238298 Oct 25 13:02 VAS_HOLD_201810261751.csv /Out/BR/SD -rw-rw-rw- 1 user group 0 Oct 25 11:24 HOLD_201810261529_S.csv -rw-rw-rw- 1 user group 1721060436 Oct 25 11:25 HOLD_201810261544_M.csv -rw-rw-rw- 1 user group 1537619643 Oct 25 11:26 HOLD_201810261546.csv -rw-rw-rw- 1 user group 1545973081 Oct 25 11:28 FINAL_201810261601.csv -rw-rw-rw- 1 user group 0 Oct 25 11:28 SUMMARY_201810261559.csv

请注意,最后增加了空间并换行了