unix – 如何在-exec中使用管道查詢
有沒有辦法在-exec中使用管道進行查詢?我不想讓grep通過整個檔案,只能通過每個檔案的第一行.
find /path/to/dir -type f -print -exec grep yourstring {} \;
我試圖將管道放在“貓”和“頭-1”,但沒有很好的工作.我試圖用某種方式使用括號,但是我並沒有想出如何把它們放在那裡.
我非常感謝你的幫助.我知道如何以其他方式工作,沒有使用發現,但我們試圖在學校使用查詢和管道,但不能管理如何.
find /path/to/dir -type f -print -exec cat {} | head -1 | grep yourstring \;
這是不知何故我們試圖做的,但是不能管理括號,甚至是可能的.我試圖通過網路來看,但是找不到任何答案.
為了能夠使用管道,您需要執行一個shell命令,即帶有管道的命令必須是-exec的單個命令.
find /path/to/dir -type f -print -exec sh -c "cat {} | head -1 | grep yourstring" \;
請注意,以上是一個無用的貓,可以寫成:
find /path/to/dir -type f -print -exec sh -c "head -1 {} | grep yourstring" \;
實現你想要的另一種方法就是說:
find /path/to/dir -type f -print -exec awk 'NR==1 && /yourstring/' {} \;
http://stackoverflow.com/questions/21825393/how-to-use-pipe-within-exec-in-find