Amit
Amit

Reputation: 11

I want to find files with extra blank line in last in linux using ksh

I am new to linux programming

I have a file which contains extra line in file.

amit>more file_20131130111111.ctl
file_20131130111111.psv|224

amit>

I want to move this type of files to another location.

And also if i can remove those line after moving it to reprocess.

Upvotes: 1

Views: 406

Answers (3)

mchelabi
mchelabi

Reputation: 154

Another possibility with awk, we check if the last line is an empty line or an empty line with spaces:

awk 'END{if($0 ~ /^$|^[ ]+$/) print "empty"}' your_file

An example of use

for file in $files 
do
     result="$(awk 'END{if($0 ~ /^$|^[ ]+$/) print "empty"}' $file)"

     if [[ "$result" == "empty" ]]
     then
         mv $file /tmp/$file
         sed -i '${/^$/d};${/^[ ]*$/d}' /tmp/$file
     fi
done

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247012

With bash, you can test if the last line of a file is empty like this:

[[ -z $(tail -1 filename) ]] && echo empty

And you can remove an empty last line with:

sed -i '${/^$/d}' filename

Upvotes: 3

brunch875
brunch875

Reputation: 869

EDIT: It seems like you want to move all files in current folder which contain a blank line to a new folder WITHOUT that blank line. Am I correct? Do do that, you can use this:

for i in $(grep -lE '[[:space:]]' *); do awk 'NF > 0' $i > newlocationfolder/$i; done

Upvotes: 0

Related Questions