【问题标题】:grep pipe sed commandsgrep 管道 sed 命令
【发布时间】:2018-04-19 17:58:08
【问题描述】:

这是“file1.txt”的统计结果

`File: 'file1.txt'
  Size: 477             Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d      Inode: 55599980    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/    marc)   Gid: ( 1000/    
marc)
Access: 2018-04-19 19:19:01.708143285 +0200
Modify: 2018-04-19 19:18:58.216117199 +0200
Change: 2018-04-19 19:18:58.216117199 +0200
Birth: -`

我想用 grep 获取“修改”行:

`stat file1.txt | grep Modify
Modify: 2018-04-19 19:18:58.216117199 +0200`

我现在想在 file1.txt 的第一行之前插入这一行。

我会使用如下命令:

`stat file1.txt | grep Modify | sed -i '1 i\"result_of_grep"' file1.txt`,

但不知道如何告诉 shell 使用前面 grep dommand 的结果(我称之为“result_of_grep”)。

提前谢谢...

【问题讨论】:

  • 为什么不在grep前面插入一行?
  • stat -c '%y' file1.txt
  • 不要为此使用sed -i。只需{ stat -c '%y' file1.txt; cat file1.txt; } > file2.txt
  • 很好,谢谢。我想在我的 file1.txt 中得到有趣的行...

标签: bash sed grep pipe


【解决方案1】:

你可以这样做:

sed -i "1i\\
#$(stat file1.txt | grep Modify)
" file1.txt

【讨论】:

  • 如果我还想在“修改”之前粘贴“#”?
猜你喜欢
  • 2011-01-17
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 2021-03-29
  • 2018-12-10
相关资源
最近更新 更多