开发者

Unix script to delete file if it contains single line

开发者 https://www.devze.com 2023-02-17 09:38 出处:网络
Consider I have a file abcde.txt which may contain one or more lines of text. I want a script that will DELETE the file if it c开发者_如何转开发ontains single line.

Consider I have a file abcde.txt which may contain one or more lines of text. I want a script that will DELETE the file if it c开发者_如何转开发ontains single line.

Something like, if 'wc -l abscde.txt' = 1 then rm abscde.txt

My system : Solaris


Here's a simple bash script:

#!/bin/bash

LINECOUNT=`wc -l abscde.txt | cut -f1 -d' '`

if [[ $LINECOUNT == 1 ]]; then
   rm -f abscde.txt
fi


delifsingleline () {
    if [ $(cat $1 | wc -l) = "1" ]
    then
        echo "Deleting $1"
        echo "rm $1"
    fi
}

Lightly tested on zsh. Should work on bash as well.


This is (mostly) just a reformat of Ben's answer:

wc -l $PATH | grep '^1 ' > /dev/null && rm -f $PATH
0

精彩评论

暂无评论...
验证码 换一张
取 消