On 2012-04-14, ExecMan <
artm...@yahoo.com> wrote:
> On Apr 14, 2:10?pm, "Wayne C. Morris" <wayne.mor...@this.is.invalid>
>> ?Eli the Bearded <*...@
eli.users.panix.com> wrote:
>>
>> > #/bin/sh
>>
>> Should be:
>>
>> #!/bin/sh
>>
>> > case "X$file" in
>> > ? ? ? ? X) echo "$0: need input file"
>> > ? ? ? ? ? ?exit 2 ;;
>> > esac
>>
>> As long as you're doing error checking, you should also make sure the file
>> exists, is a regular file, and is readable:
>>
>> if [ ! -f "$file" ] || [ ! -r "$file" ] ; then
>> ? ? echo "$0: input file is not a regular file or is not readable"
>> ? ? exit 2 ;
>> fi
>>
>> > case "X$num" in
>> > ? ?# solaris /bin/sh wouldn't like something like "X[^1-9]"
>> > ? ? ? ? X[1-9]) : okay ;;
>> > ? ? ? ? X*) echo "$0: need number of files"
>> > ? ? ? ? ? ?exit 2 ;;
>> > esac
>>
>> That won't allow more than 9 files. ?Untested on Solaris, but this should work:
>>
>> case "X$num" in
>> ? ? X*[!0-9]*) echo "$0: need number of files"
>> ? ? ? ? exit 2 ;;
>> ? # test for all zeroes
>> ? ? X*[1-9]*) : okay ;;
>> ? ? X*) echo "$0: number of files cannot be zero"
>> ? ? ? ? exit 2 ;;
>> esac
>>
>> > wcout=`wc -l < "$file"` # has whitespace
>> > lines=`expr $wcout + 0` # fix whitespace for solaris expr
>>
>> > case "X$lines" in
>> > ? ? ? ? X[1-9]*) : okay ;;
>> > ? ? ? ? X*) echo "$0: unexpected output from wc"
>> > ? ? ? ? ? ? exit 2 ;;
>> > esac
>>
>> That tests the output from expr, not wc. ?I'm not sure a test is necessary
>> anyway; is there any situation where wc could give unexpected output if $file is
>> a regular file and can be read? ?If not, I'd just write:
>>
>> lines=`wc -l < "$file"`
>>
>> > split=`expr "$lines" / "$num"`
>>
>> > # deal with expr rounding the wrong way
>> > total=`expr $split \* $num`
>> > if expr "$total" \< "$lines" > /dev/null ; then
>> > ? split=`expr $split + 1`
>> > fi
>>
>> Here's a simpler way to divide and round up:
>>
>> split=`expr \( $lines + $num - 1 \) / $num`
>
>
> I found this line on a website and it works:
>
> awk '{a[NR]=$0}END{for(i=1;i<=NR;i++)print a[i] > "tmp_"
> 1+int(n*((i-1)/NR))}' n=$msgs $pfpfile
>
> But, I do not understand what is it doing. I see that NR, what is
> that?
NR = number record (ie line number) This sets up an associative array
within awk where each line is one member of the array. Thos lines are
then printed out to the file tmp_ # where # is the number of the file(
which now has a black in its name.)
Ie, This is extremely inefficient of space, and could well croak on a
long file as the whole file is copied into memory.
Note that all of these will fail pretty miserably if one of the lines is
say 99% of the length of the file, and the other 500 lines are each two
characters long.
>