我试图使用查找-exec与多个命令没有任何成功。有人知道像下面这样的命令是否可行吗?
find *.txt -exec echo "$(tail -1 '{}'),$(ls '{}')" \;
基本上,我试图打印当前目录中每个txt文件的最后一行,并打印在行末,逗号后跟文件名。
我试图使用查找-exec与多个命令没有任何成功。有人知道像下面这样的命令是否可行吗?
find *.txt -exec echo "$(tail -1 '{}'),$(ls '{}')" \;
基本上,我试图打印当前目录中每个txt文件的最后一行,并打印在行末,逗号后跟文件名。
当前回答
下列其中一项:
find *.txt -exec awk 'END {print $0 "," FILENAME}' {} \;
find *.txt -exec sh -c 'echo "$(tail -n 1 "$1"),$1"' _ {} \;
find *.txt -exec sh -c 'echo "$(sed -n "\$p" "$1"),$1"' _ {} \;
其他回答
下列其中一项:
find *.txt -exec awk 'END {print $0 "," FILENAME}' {} \;
find *.txt -exec sh -c 'echo "$(tail -n 1 "$1"),$1"' _ {} \;
find *.txt -exec sh -c 'echo "$(sed -n "\$p" "$1"),$1"' _ {} \;
感谢Camilo Martin,我得以回答一个相关的问题:
我想做的是
find ... -exec zcat {} | wc -l \;
但这并不奏效。然而,
find ... | while read -r file; do echo "$file: `zcat $file | wc -l`"; done
确实有用,所以谢谢!
find . -type d -exec sh -c "echo -n {}; echo -n ' x '; echo {}" \;
应该使用xargs:)
find *.txt -type f -exec tail -1 {} \; | xargs -ICONSTANT echo $(pwd),CONSTANT
另一个(在osx上工作)
find *.txt -type f -exec echo ,$(PWD) {} + -exec tail -1 {} + | tr ' ' '/'
我找到了这个解决方案(可能已经在评论中说过了,但我找不到任何答案)
你可以使用bash -c在一行中执行多个命令
find . <SOMETHING> -exec bash -c "EXECUTE 1 && EXECUTE 2 ; EXECUTE 3" \;
在你的情况下
find . -name "*.txt" -exec bash -c "tail -1 '{}' && ls '{}'" \;
我用一个测试文件测试了它:
[gek@tuffoserver tmp]$ ls *.txt
casualfile.txt
[gek@tuffoserver tmp]$ find . -name "*.txt" -exec bash -c "tail -1 '{}' && ls '{}'" \;
testonline1=some TEXT
./casualfile.txt