LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-17-2009, 02:02 AM   #1
kicap
LQ Newbie
 
Registered: Nov 2004
Posts: 3

Rep: Reputation: 0
Looping and Grouping the values


Hi all

I need someone help and appreciated if you guys can hands on me.

I have txt file that contains a value 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12

and i just want to group all of them following this format;

a:1, 2, 3, 4
b:5, 6, 7, 8
c:9, 10, 11, 12

and i have write a script like this;

#!/bin/sh

file=test.csv;
var=`cat $file`

LIMIT=3
i=0

while [ "$i" -lt "$LIMIT" ]
do

i=$(($i+1))
echo -n a:"$i "
if [ $i -eq $LIMIT ]
then
i=$(($i+1))
echo -n b:"$i "
fi
i=$(($i+1))
echo c:$i
done

But still cannot create an output that i needed. Hope someone here can help me to figured it out..

Million Thanks
 
Old 09-17-2009, 02:32 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by kicap View Post
Hi all

I need someone help and appreciated if you guys can hands on me.

I have txt file that contains a value 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12

and i just want to group all of them following this format;

a:1, 2, 3, 4
b:5, 6, 7, 8
c:9, 10, 11, 12

and i have write a script like this;

#!/bin/sh

file=test.csv;
var=`cat $file`

LIMIT=3
i=0

while [ "$i" -lt "$LIMIT" ]
do

i=$(($i+1))
echo -n a:"$i "
if [ $i -eq $LIMIT ]
then
i=$(($i+1))
echo -n b:"$i "
fi
i=$(($i+1))
echo c:$i
done

But still cannot create an output that i needed. Hope someone here can help me to figured it out..

Million Thanks
Try this:

Code:
data=(`cat data.txt`)

i=0
rowlength=4

while [ $i -lt ${#data[*]} ]
do
   echo -n "${data[$i]} "
   [ $((++i % rowlength)) -eq 0 ] && echo ""
done
BUT ... this script has a bug, and I want you to discover what it is and fix it. If you don't, your instructor (who gave you this homework assignment) will know you got this from the Web and didn't bother to check it carefully enough. And who knows where that might lead -- you might end up living in a trailer with a very fat partner and 14 dogs, listening to Country & Western music. (shudder)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Software Sections/Grouping prakashpms Linux - Software 3 08-19-2009 07:27 PM
Need help with grouping script Dan_86 Linux - Newbie 1 04-30-2008 02:16 AM
grouping variable locations taras masnyj Programming 2 10-02-2006 08:11 PM
Squid User Grouping dan0r Linux - Software 1 03-06-2006 11:14 AM
Gnome Grouping Windows maelstrom209 Linux - Software 1 12-25-2004 06:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration