忘记密码   免费注册 查看新帖 |

ChinaUnix.net

  平台 论坛 博客 文库 频道自动化运维 虚拟化 储存备份 C/C++ PHP MySQL 嵌入式 Linux系统
最近访问板块 发新帖
查看: 598 | 回复: 4

[文本处理] sed: 文本文件的第二行依次和下面的每一行合并成一行 [复制链接]

论坛徽章:
0
发表于 2018-10-09 18:56 |显示全部楼层
本帖最后由 cgiyan 于 2018-10-09 19:09 编辑

sed: 文本文件的第二行依次和下面的每一行合并成一行

我的日志文件格式如下:
  1. <blockquote>            PID |VSSSIZE    |RSSSIZE
复制代码


我想让文件的第二行依次和下面的每一行合并成一行 如下:

  1. 18-09-29 11:01:07  serv_life  |12937  |126.215MB  |22.7188MB|
复制代码




我使用sed ‘N;s/\n//’  test.log,  如何能依次合并?


谢谢!


论坛徽章:
0
发表于 2018-10-09 19:10 |显示全部楼层
原始日志文件格式如下:

            PID |VSSSIZE    |RSSSIZE
18-09-29 11:01:07
serv_life  |12937  |126.215MB  |22.7188MB|
serv_life  |12932  |126.203MB  |22.9062MB|
serv_life  |12934  |126.199MB  |22.7891MB|
serv_life  |12940  |126.195MB  |22.6719MB|
serv_life  |12941  |126.191MB  |22.7461MB|
serv_que   |12928  |125.547MB  |19.4297MB|
serv_que   |12930  |125.547MB  |19.3984MB|
serv_que   |12931  |125.543MB  |19.4531MB|
serv_que   |12927  |125.543MB  |19.4492MB|
serv_que   |12929  |125.543MB  |19.4492MB|
serv_life  |12938  |125.137MB  |22.7188MB|
serv_life  |12933  |125.129MB  |22.6445MB|
serv_life  |12936  |125.125MB  |22.7656MB|
serv_life  |12939  |125.121MB  |22.6484MB|
serv_life  |12935  |125.105MB  |22.6445MB|
serv_bat   |12943  |121.027MB  |17.1953MB|
serv_bat   |12942  |121.023MB  |17.1914MB|
serv_auto  |12918  |120.754MB  |18.2344MB|
TxnMCtoSD  |26347  |27.7539MB  |1.49609MB|
TxnMCtoSD  |26345  |27.7539MB  |1.49609MB|
TxnMCtoSD  |26344  |25.6367MB  |1.46875MB|
TxnMCtoSD  |12916  |22.4375MB  |1.23047MB|
TxnFCtoSD  |12917  |21.5195MB  |1.19141MB|
TxnRunD    |12914  |20.3359MB  |1.52344MB|
TxnMonD    |12915  |19.4453MB  |1.16016MB|
------------------------------------------
              PID |VSSSIZE    |RSSSIZE
18-09-29 11:03:07
serv_life  |12932  |126.203MB  |22.9023MB|
serv_life  |12933  |126.203MB  |22.6758MB|
serv_life  |12936  |126.199MB  |22.8008MB|
serv_life  |12940  |126.195MB  |22.6719MB|
serv_que   |12928  |125.547MB  |19.4297MB|
serv_que   |12930  |125.547MB  |19.3984MB|
serv_que   |12931  |125.543MB  |19.4531MB|
serv_que   |12929  |125.543MB  |19.4492MB|
serv_que   |12927  |125.543MB  |19.4492MB|
serv_life  |12938  |125.137MB  |22.7188MB|
serv_life  |12934  |125.129MB  |22.7617MB|
serv_life  |12937  |125.129MB  |22.6914MB|
serv_life  |12939  |125.121MB  |22.6484MB|
serv_life  |12941  |125.117MB  |22.7188MB|
serv_life  |12935  |125.105MB  |22.6445MB|
serv_bat   |12943  |121.027MB  |17.1953MB|
serv_bat   |12942  |121.023MB  |17.1914MB|
serv_auto  |12918  |120.754MB  |18.2344MB|
TxnMCtoSD  |12916  |22.4375MB  |1.23047MB|
TxnFCtoSD  |12917  |21.5195MB  |1.19141MB|
TxnRunD    |12914  |20.3359MB  |1.52344MB|
TxnMonD    |12915  |19.4453MB  |1.16016MB|

论坛徽章:
19
程序设计版块每日发帖之星
日期:2015-10-11 06:20:0015-16赛季CBA联赛之浙江
日期:2018-07-14 13:27:4015-16赛季CBA联赛之同曦
日期:2018-04-17 12:43:3415-16赛季CBA联赛之福建
日期:2018-02-09 14:28:3315-16赛季CBA联赛之四川
日期:2017-09-04 12:27:0315-16赛季CBA联赛之青岛
日期:2017-06-26 18:30:0315-16赛季CBA联赛之新疆
日期:2017-04-12 22:55:4715-16赛季CBA联赛之山东
日期:2016-05-28 18:18:5615-16赛季CBA联赛之新疆
日期:2016-05-07 05:05:3215-16赛季CBA联赛之八一
日期:2016-03-14 12:32:06IT运维版块每日发帖之星
日期:2016-02-27 06:20:0015-16赛季CBA联赛之深圳
日期:2016-01-31 10:37:36
发表于 2018-10-10 09:57 |显示全部楼层
  1. # awk '/^[0-9]+/{a=$0;next}/^\w+/{print a,$0}' file
  2. 18-09-29 11:01:07 serv_life  |12937  |126.215MB  |22.7188MB|
  3. 18-09-29 11:01:07 serv_life  |12932  |126.203MB  |22.9062MB|
  4. 18-09-29 11:01:07 serv_life  |12934  |126.199MB  |22.7891MB|
  5. 18-09-29 11:01:07 serv_life  |12940  |126.195MB  |22.6719MB|
  6. 18-09-29 11:01:07 serv_life  |12941  |126.191MB  |22.7461MB|
  7. 18-09-29 11:01:07 serv_que   |12928  |125.547MB  |19.4297MB|
  8. 18-09-29 11:01:07 serv_que   |12930  |125.547MB  |19.3984MB|
  9. 18-09-29 11:01:07 serv_que   |12931  |125.543MB  |19.4531MB|
  10. 18-09-29 11:01:07 serv_que   |12927  |125.543MB  |19.4492MB|
  11. 18-09-29 11:01:07 serv_que   |12929  |125.543MB  |19.4492MB|
  12. 18-09-29 11:01:07 serv_life  |12938  |125.137MB  |22.7188MB|
  13. 18-09-29 11:01:07 serv_life  |12933  |125.129MB  |22.6445MB|
  14. 18-09-29 11:01:07 serv_life  |12936  |125.125MB  |22.7656MB|
  15. 18-09-29 11:01:07 serv_life  |12939  |125.121MB  |22.6484MB|
  16. 18-09-29 11:01:07 serv_life  |12935  |125.105MB  |22.6445MB|
  17. 18-09-29 11:01:07 serv_bat   |12943  |121.027MB  |17.1953MB|
  18. 18-09-29 11:01:07 serv_bat   |12942  |121.023MB  |17.1914MB|
  19. 18-09-29 11:01:07 serv_auto  |12918  |120.754MB  |18.2344MB|
  20. 18-09-29 11:01:07 TxnMCtoSD  |26347  |27.7539MB  |1.49609MB|
  21. 18-09-29 11:01:07 TxnMCtoSD  |26345  |27.7539MB  |1.49609MB|
  22. 18-09-29 11:01:07 TxnMCtoSD  |26344  |25.6367MB  |1.46875MB|
  23. 18-09-29 11:01:07 TxnMCtoSD  |12916  |22.4375MB  |1.23047MB|
  24. 18-09-29 11:01:07 TxnFCtoSD  |12917  |21.5195MB  |1.19141MB|
  25. 18-09-29 11:01:07 TxnRunD    |12914  |20.3359MB  |1.52344MB|
  26. 18-09-29 11:01:07 TxnMonD    |12915  |19.4453MB  |1.16016MB|
  27. 18-09-29 11:03:07 serv_life  |12932  |126.203MB  |22.9023MB|
  28. 18-09-29 11:03:07 serv_life  |12933  |126.203MB  |22.6758MB|
  29. 18-09-29 11:03:07 serv_life  |12936  |126.199MB  |22.8008MB|
  30. 18-09-29 11:03:07 serv_life  |12940  |126.195MB  |22.6719MB|
  31. 18-09-29 11:03:07 serv_que   |12928  |125.547MB  |19.4297MB|
  32. 18-09-29 11:03:07 serv_que   |12930  |125.547MB  |19.3984MB|
  33. 18-09-29 11:03:07 serv_que   |12931  |125.543MB  |19.4531MB|
  34. 18-09-29 11:03:07 serv_que   |12929  |125.543MB  |19.4492MB|
  35. 18-09-29 11:03:07 serv_que   |12927  |125.543MB  |19.4492MB|
  36. 18-09-29 11:03:07 serv_life  |12938  |125.137MB  |22.7188MB|
  37. 18-09-29 11:03:07 serv_life  |12934  |125.129MB  |22.7617MB|
  38. 18-09-29 11:03:07 serv_life  |12937  |125.129MB  |22.6914MB|
  39. 18-09-29 11:03:07 serv_life  |12939  |125.121MB  |22.6484MB|
  40. 18-09-29 11:03:07 serv_life  |12941  |125.117MB  |22.7188MB|
  41. 18-09-29 11:03:07 serv_life  |12935  |125.105MB  |22.6445MB|
  42. 18-09-29 11:03:07 serv_bat   |12943  |121.027MB  |17.1953MB|
  43. 18-09-29 11:03:07 serv_bat   |12942  |121.023MB  |17.1914MB|
  44. 18-09-29 11:03:07 serv_auto  |12918  |120.754MB  |18.2344MB|
  45. 18-09-29 11:03:07 TxnMCtoSD  |12916  |22.4375MB  |1.23047MB|
  46. 18-09-29 11:03:07 TxnFCtoSD  |12917  |21.5195MB  |1.19141MB|
  47. 18-09-29 11:03:07 TxnRunD    |12914  |20.3359MB  |1.52344MB|
  48. 18-09-29 11:03:07 TxnMonD    |12915  |19.4453MB  |1.16016MB|
复制代码

评分

参与人数 1信誉积分 +50 收起 理由
cgiyan + 50 赞一个!

查看全部评分

论坛徽章:
0
发表于 2018-10-10 10:36 |显示全部楼层
查找  ..:..:..回车  ,替换成没有回车即可。“.”代表正则中的任意字符。

论坛徽章:
19
程序设计版块每日发帖之星
日期:2016-05-03 06:20:0015-16赛季CBA联赛之山西
日期:2018-08-07 19:46:2315-16赛季CBA联赛之佛山
日期:2018-08-03 13:19:33黑曼巴
日期:2018-07-06 15:19:5015-16赛季CBA联赛之八一
日期:2018-07-05 10:34:0915-16赛季CBA联赛之八一
日期:2018-07-03 16:56:4615-16赛季CBA联赛之深圳
日期:2018-06-15 14:59:3715-16赛季CBA联赛之青岛
日期:2018-06-08 13:45:2815-16赛季CBA联赛之同曦
日期:2018-06-04 19:42:2015-16赛季CBA联赛之山东
日期:2018-05-30 12:44:59CU十四周年纪念徽章
日期:2018-05-15 11:36:3815-16赛季CBA联赛之广东
日期:2018-05-14 09:52:42
发表于 2018-10-10 10:40 |显示全部楼层
本帖最后由 wh7211 于 2018-10-10 11:18 编辑

回复 1# cgiyan

  1. awk '/^[0-9]/{a=$0;next}{print $0~/PID|---/?$0:a" "$0}' file
  2. sed -nr '/PID|---/{p;d};/^[0-9]/{h;d};G;s/(.*)\n(.*)/\2 \1/p;' file
复制代码

输出:
  1.             PID |VSSSIZE    |RSSSIZE
  2. 18-09-29 11:01:07 serv_life  |12937  |126.215MB  |22.7188MB|
  3. 18-09-29 11:01:07 serv_life  |12932  |126.203MB  |22.9062MB|
  4. 18-09-29 11:01:07 serv_life  |12934  |126.199MB  |22.7891MB|
  5. 18-09-29 11:01:07 serv_life  |12940  |126.195MB  |22.6719MB|
  6. 18-09-29 11:01:07 serv_life  |12941  |126.191MB  |22.7461MB|
  7. 18-09-29 11:01:07 serv_que   |12928  |125.547MB  |19.4297MB|
  8. 18-09-29 11:01:07 serv_que   |12930  |125.547MB  |19.3984MB|
  9. 18-09-29 11:01:07 serv_que   |12931  |125.543MB  |19.4531MB|
  10. 18-09-29 11:01:07 serv_que   |12927  |125.543MB  |19.4492MB|
  11. 18-09-29 11:01:07 serv_que   |12929  |125.543MB  |19.4492MB|
  12. 18-09-29 11:01:07 serv_life  |12938  |125.137MB  |22.7188MB|
  13. 18-09-29 11:01:07 serv_life  |12933  |125.129MB  |22.6445MB|
  14. 18-09-29 11:01:07 serv_life  |12936  |125.125MB  |22.7656MB|
  15. 18-09-29 11:01:07 serv_life  |12939  |125.121MB  |22.6484MB|
  16. 18-09-29 11:01:07 serv_life  |12935  |125.105MB  |22.6445MB|
  17. 18-09-29 11:01:07 serv_bat   |12943  |121.027MB  |17.1953MB|
  18. 18-09-29 11:01:07 serv_bat   |12942  |121.023MB  |17.1914MB|
  19. 18-09-29 11:01:07 serv_auto  |12918  |120.754MB  |18.2344MB|
  20. 18-09-29 11:01:07 TxnMCtoSD  |26347  |27.7539MB  |1.49609MB|
  21. 18-09-29 11:01:07 TxnMCtoSD  |26345  |27.7539MB  |1.49609MB|
  22. 18-09-29 11:01:07 TxnMCtoSD  |26344  |25.6367MB  |1.46875MB|
  23. 18-09-29 11:01:07 TxnMCtoSD  |12916  |22.4375MB  |1.23047MB|
  24. 18-09-29 11:01:07 TxnFCtoSD  |12917  |21.5195MB  |1.19141MB|
  25. 18-09-29 11:01:07 TxnRunD    |12914  |20.3359MB  |1.52344MB|
  26. 18-09-29 11:01:07 TxnMonD    |12915  |19.4453MB  |1.16016MB|
  27. ------------------------------------------
  28.               PID |VSSSIZE    |RSSSIZE
  29. 18-09-29 11:03:07 serv_life  |12932  |126.203MB  |22.9023MB|
  30. 18-09-29 11:03:07 serv_life  |12933  |126.203MB  |22.6758MB|
  31. 18-09-29 11:03:07 serv_life  |12936  |126.199MB  |22.8008MB|
  32. 18-09-29 11:03:07 serv_life  |12940  |126.195MB  |22.6719MB|
  33. 18-09-29 11:03:07 serv_que   |12928  |125.547MB  |19.4297MB|
  34. 18-09-29 11:03:07 serv_que   |12930  |125.547MB  |19.3984MB|
  35. 18-09-29 11:03:07 serv_que   |12931  |125.543MB  |19.4531MB|
  36. 18-09-29 11:03:07 serv_que   |12929  |125.543MB  |19.4492MB|
  37. 18-09-29 11:03:07 serv_que   |12927  |125.543MB  |19.4492MB|
  38. 18-09-29 11:03:07 serv_life  |12938  |125.137MB  |22.7188MB|
  39. 18-09-29 11:03:07 serv_life  |12934  |125.129MB  |22.7617MB|
  40. 18-09-29 11:03:07 serv_life  |12937  |125.129MB  |22.6914MB|
  41. 18-09-29 11:03:07 serv_life  |12939  |125.121MB  |22.6484MB|
  42. 18-09-29 11:03:07 serv_life  |12941  |125.117MB  |22.7188MB|
  43. 18-09-29 11:03:07 serv_life  |12935  |125.105MB  |22.6445MB|
  44. 18-09-29 11:03:07 serv_bat   |12943  |121.027MB  |17.1953MB|
  45. 18-09-29 11:03:07 serv_bat   |12942  |121.023MB  |17.1914MB|
  46. 18-09-29 11:03:07 serv_auto  |12918  |120.754MB  |18.2344MB|
  47. 18-09-29 11:03:07 TxnMCtoSD  |12916  |22.4375MB  |1.23047MB|
  48. 18-09-29 11:03:07 TxnFCtoSD  |12917  |21.5195MB  |1.19141MB|
  49. 18-09-29 11:03:07 TxnRunD    |12914  |20.3359MB  |1.52344MB|
  50. 18-09-29 11:03:07 TxnMonD    |12915  |19.4453MB  |1.16016MB|
复制代码

评分

参与人数 1信誉积分 +30 收起 理由
cgiyan + 30

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号 北京市公安局海淀分局网监中心备案编号:11010802020122
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员  联系我们:wangnan@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP