Linux:删除不包含特定行数的文件

Linux:删除不包含特定行数的文件,第1张

概述如何删除目录中具有多于或少于指定行数的文件(所有文件都有“.txt”后缀)?最佳答案这个bash脚本应该可以解决问题.保存为“rmlc.sh”.样品用法:rmlc.sh -more 20 *.txt # Remove all .txt files with more than 20 lines rmlc.sh -less 15 * # Rem

如何删除目录中具有多于或少于指定行数的文件(所有文件都有“.txt”后缀)?最佳答案这个bash脚本应该可以解决问题.保存为“rmlc.sh”.

样品用法:

rmlc.sh -more 20 *.txt   # Remove all .txt files with more than 20 linesrmlc.sh -less 15 *       # Remove ALL files with fewer than 15 lines

请注意,如果rmlc.sh脚本位于当前目录中,则会对其进行保护以防删除.

#!/bin/sh# rmlc.sh - Remove by line countSCRIPTname="rmlc.sh"IFS=""# Parse arguments if [ $# -lt 3 ]; then    echo "Usage:"    echo "$SCRIPTname [-more|-less] [numlines] file1 file2..."    exit fiif [  == "-more" ]; then    COMPARE="-gt" elif [  == "-less" ]; then    COMPARE="-lt" else    echo "First argument must be -more or -less"    exit filiNECOUNT=# discard non-filename argumentsshift 2for filename in $*; do    # Make sure we're dealing with a regular file first    if [ ! -f "$filename" ]; then        echo "Ignoring $filename"        continue    fi    # We probably don't want to delete ourselves if script is in current dir    if [ "$filename" == "$SCRIPTname" ]; then        continue    fi    # Feed wc with stdin so that output doesn't include filename    lines=`cat "$filename" | wc -l`    # Check criteria and delete    if [ $lines $COMPARE $liNECOUNT ]; then        echo "Deleting $filename"        rm "$filename"    fi done
总结

以上是内存溢出为你收集整理的Linux:删除不包含特定行数的文件全部内容,希望文章能够帮你解决Linux:删除不包含特定行数的文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1047884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存