【问题标题】:how to capture the 1st command output in terminal and to print that variable using the last command in the same line如何在终端中捕获第一个命令输出并使用同一行中的最后一个命令打印该变量
【发布时间】:2016-09-15 11:25:12
【问题描述】:

实际输出为

$ grep -Hcw count copy_hb_script 
copy_hb_script:5

我正在使用以下命令来获得预期的输出,但我失败了

grep -Hcw count copy_hb_script | awk '{print $1}' |xargs ls -ld | awk '{print $8 " " $9 }'

出去是

03:49 copy_hb_script 

缺少文件计数,是否有任何替代方法可以获取带有文件计数的时间戳,如下所示

03:49 copy_hb_script:5

【问题讨论】:

  • 请提供minimal reproducible example,否则这个不是很清楚
  • 这看起来像是有人将他们在互联网上找到的命令拼凑在一起,而没有了解这些命令的用途。例如你使用grep -Hcw,然后你(尝试)去掉 -H 部分。只是不要使用-H。您正在使用 xargs,但您正在返回一个文件名。了解您在管道中使用的每个命令。
  • @SaintHax:请查看@lnian 的答案以了解我在看什么。

标签: linux bash terminal grep


【解决方案1】:

can avoid parsing ls output 并在 bash 中使用 stat 命令获取详细的文件信息。

 #  'stat -c' produces output as 2016-09-15 16:03:40.655456000 +0530
 #   Stripping off extra information after the '.' using string-manipulation            
 #   Running the grep with the count together with the previous command

 modDate=$(stat -c %y copy_hb_script); echo "${modDate%.*}" "$(grep -Hcw count copy_hb_script)"

产生一个输出为

016-09-15 16:03:40 copy_hb_script:5

【讨论】:

    猜你喜欢
    • 2013-08-30
    • 1970-01-01
    • 2017-06-20
    • 2019-12-04
    • 2011-08-22
    • 2021-01-31
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多