0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

findで見つけた複数ファイルの最終行を削除する

Posted at

問題

  • これをしたい。
$ cat sample.txt
hoge1
hoge2
hoge3
(空白)
(空白)
↓
$ cat sample.txt
hoge1
hoge2
hoge3
(空白)
  • ディレクトリ配下に複数のsample_*.txtがあるような状況を考え、その全てに上記の処理をしたい。

ダメだった例

  • 以下の方法でできるかと思ったがなぜかダメだった。
    • 理由わかる方、ぜひコメントで教えてください!
find ${TGT_DIR} -maxdepth 1 -name "*.txt" | xargs -I {} bash -c "sed -i -e '$d' '{}'"

うまくいった例

  • whileで回す方法だと、期待通りの動作となった。
find ${TGT_DIR} -maxdepth 1 -name "*.txt" | while read file; do
    echo ${file}
    sed -i -e '$d' ${file}
done
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?