#!/bin/bash # This script checks all arguments (or if no arguments are given, all files named *.dat in the directory), # with the exception of records.dat, # and prompts for cutting lines and characters in excess # i.e. cuts lines after the $MAX_LINES-th line and characters after the $MAX_CHARS-th character in each line # The script also adds as many blank spaces as needed to even the character count on each line # i.e. in the and each line will have $LONGEST_LINE characters in total # Old boss.dat file is copied to boss.dat.old to prevent data loss. I'm aware this is a coward's walkaround # to the fact this script has issues. # KNOWN BUGS: # - there is no return value checking from the functions called # - first bug implies that boss.dat file could be substituted with an empty file, or worse # Please report any bugs or suggestions to me .D #wc -L is the length of the longest line #wc -m is the character count for the file #wc -l is the line count for the file MAX_CHARS=10 MAX_LINES=10 FILES="" if [ $# -eq 1 -a $1 == "-h" ]; then echo Usage: $0 \.; exit 1; fi if [ $# -gt 0 ]; then #check that file exists for k in $@; do if [ ! -e $k -a ! $k.dat ]; then echo $k does not exist! echo ignoring argument continue else #check that file has .dat extension if [[ "$k" =~ dat$ ]]; then FILES="$FILES $k" else if [[ "Sk.dat" =~ dat$ ]]; then echo File $k is not a .dat, but a file named $k.dat exists. Did you mean that file? \(Y/N\) read ans if [ $ans != "y" -a $ans != "Y" ]; then exit 1 else FILES="$FILES $k.dat" continue fi else echo $k does not look like a .dat file echo ignoring argument continue fi fi fi done else FILES=`ls *.dat 2> /dev/null` fi echo $FILES > args.tmp #FOR EACH boss.dat FILE for i in `cat args.tmp`; do if [ $i == "records.dat" -o $i == "notes.dat" ]; then continue; fi #MAKE A BACKUP COPY cp $i $i.old #CUT LINES IN EXCESS N_LINES=`wc -l $i | cut -f1 -d' '` if [ $N_LINES -gt $MAX_LINES ]; then echo file $i has too many lines \($N_LINES\): cut lines in excess? Y\\N read ans while [ $ans != 'y' -a $ans != 'Y' -a $ans != 'n' -a $ans != 'N' ]; do echo cut lines in excess? Y\\N read ans done if [ $ans == 'y' -o $ans == 'Y' ]; then head -$MAX_LINES $i > output.tmp mv output.tmp $i N_LINES=`wc -l $i | cut -f1 -d' '` echo file has now $N_LINES lines fi echo fi #CUT CHARACTERS IN EXCESS LONGEST_LINE=`wc -L $i | cut -f1 -d' '` if [ $LONGEST_LINE -gt $MAX_CHARS ]; then echo file $i has at least one line that is longer than permitted. max length is $MAX_CHARS echo cut characters in excess? Y\\N read ans while [ $ans != 'y' -a $ans != 'Y' -a $ans != 'n' -a $ans != 'N' ]; do echo cut lines in excess? Y\\N read ans done if [ $ans == 'y' -o $ans == 'Y' ]; then cat $i | cut -c1-$MAX_CHARS > output.tmp2 mv output.tmp2 $i LONGEST_LINE=`wc -L $i | cut -f1 -d' '` echo longest line has now $LONGEST_LINE characters fi echo fi #ADD BLANK SPACES AT THE END OF LINES for j in `seq 1 $MAX_CHARS`; do sed 's/.*/& /' $i > output.tmp3 mv output.tmp3 $i done cat $i | cut -c1-$LONGEST_LINE > output.tmp4 mv output.tmp4 $i done rm args.tmp