[SATLUG] Shell scripting challenge

Enrique Sanchez Vela esanchezvela at yahoo.com
Mon Apr 6 10:58:27 CDT 2009


> --- On Mon, 4/6/09, Emon <emon at lavabit.com>
> wrote:
> 
> > From: Emon <emon at lavabit.com>
> > Subject: [SATLUG] Shell scripting challenge
> > To: "The San Antonio Linux User's Group Mailing List"
> <satlug at satlug.org>
> > Date: Monday, April 6, 2009, 12:26 AM
> > Hi all
> > 
> > I am studying for RHCE exam. The instructor is a
> friend of
> > mine, and he
> > has issued me an off topic challenge. The challenge
> is
> > …....
> > 
> > To write a shell script that will take a given text
> file as
> > input &
> > divide into five equal but different files. If the
> number
> > of lines in
> > the file is not perfectly divisible by five,
> then  the
> > last file should
> > contain the extra lines; i.e. if the total number of
> lines
> > in the
> > original file is 16, then it should divide them into
> 5
> > files, where the
> > first 4 files should contain 3 lines each & the
> fifth
> > file should
> > contain 4 lines.
> > 
> > I have some very basic (Bash) shell scripting &
> (C++)
> > programming
> > concept, but shamefully I must admit that I can't
> figure
> > out very much.
> > 
> > All I have figured out is that I should use "wc -l"
> command
> > to find out
> > the number of lines in the file & store it in a
> > variable & then divide
> > it by 5; but after that how should I store the
> quotient
> > & the
> > reminder??..... and how do I use those numbers to
> process
> > the file???
> > 
> > Any help/pointer would be very much appreciated.
> > 
> > Emon
> > 
> > -- 
> > _______________________________________________
> > SATLUG mailing list
> > SATLUG at satlug.org
> > http://alamo.satlug.org/mailman/listinfo/satlug to
> > unsubscribe
> > Powered by Rackspace (www.rackspace.com)
> > 
> 
> rm -f xxf
> lc=$(wc -l file| awk '{print $1}')
> ((j=lc/5))
> split -l $j file
> if [ -f xxf ] ; then cat xxf >> xxe ; rm xxf; fi
> 


this also works....


lc=$(wc -l file| awk '{print $1}')
((j=lc/5))
for i in 1 2 3 4
do
((x=j*i))
head -$x file | tail -$j >> file_$i
done
((x=x+1))
tail +$x file >> file_5




      


More information about the SATLUG mailing list