Reputation: 11
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
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
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
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