【问题标题】:Is it possible to parse standard error without writing out to a file first? [duplicate]是否可以在不先写入文件的情况下解析标准错误? [复制]
【发布时间】:2014-02-03 07:40:25
【问题描述】:

在一个简单的情况下,假设我们有一些标准错误:

$ ls /fake/file
ls: /fake/file: No such file or directory

问题:是否可以从标准错误中解析出“/fake/file”而无需先将其写入文件?例如:

$ ls /fake/file 2> tmp.file; sed 's/.* \(.*\):.*/\1/' tmp.file
/fake/file

【问题讨论】:

标签: bash sed awk


【解决方案1】:

这样的?

ls /fake/file 2>&1 | awk -F: '{print $2}'

【讨论】:

  • 解析不完全正确,但是:ls /fake/file 2>&1 是我正在寻找的。谢谢!
【解决方案2】:

无论哪种方式都应该为您获取文件名

ls /fake/file 2>&1 | awk -F: '{print $2}' | awk '{print $3}'

ls /fake/file 2>&1 | awk '{print $4}' | awk -F: '{print $1}'

ls /fake/file 2>&1 | sed 's/.* \(.*\):.*/\1/'

【讨论】:

  • 我不认为这两个 awk 解决方案可以按您的预期工作。
猜你喜欢
  • 2013-05-03
  • 2015-04-25
  • 2014-04-01
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
  • 2010-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多