while loop DATA


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers while loop DATA
# 1  
Old 01-15-2007
Data while loop DATA

I have a datafile contain:

1;n;b;c
2;n;b;c
3;n;b;c
4;n;b;c
5;n;b;c
6;n;b;c
7;n;b;c
8;n;b;c
9;n;b;c
10;n;b;c

My program code:

cnt=1
IFS=";"
while read a b c d; do
echo $a $b $c $d
done<datafile

My result:

1 n b c
2 n b c
3 n b c
4 n b c
5 n b c
6 n b c
7 n b c
8 n b c
9 n b c
10 n b c

My wanted-result

1 n b c list[1] row[1]
2 n b c list[1] row[2]
3 n b c list[1] row[3]
4 n b c list[1] row[4]
5 n b c list[1] row[5]
6 n b c list[2] row[1]
7 n b c list[2] row[2]
8 n b c list[2] row[3]
9 n b c list[2] row[4]
10 n b c list[2] row[5]

How can I modify my code to give me such data output? Thanks!

Row 1 to 5 is in list 1
Row 6 to 10 is in list 2
# 2  
Old 01-16-2007
if you change the list after every 5 rows them try somthing like this.

In basic shell

#!/usr/bin/sh
row=1
list=1
IFS=";"
while read a b c d; do
echo "$a $b $c $d list[$list] row[$row]"
row=`expr $row + 1`
list=`expr $list + 1`
if [ $row -eq 6 ]
then
row=1
fi
if [ $list -eq 6 ]
then
list=1
fi

done<datafile
# 3  
Old 01-16-2007
OOPS - The codes not quite right.

#!/usr/bin/sh
row=1
list=1
IFS=";"
while read a b c d; do
echo "$a $b $c $d list[$list] row[$row]"
row=`expr $row + 1`
if [ $row -eq 6 ]
then
row=1
list=`$list + 1`
fi
if [ $list -eq 6 ]
then
list=1
fi
# 4  
Old 01-16-2007
Code:
awk -F";" -v no=1 ' {  print $1,$2,$3,$4" list[" ( NR%5 != 0 ? no:no++ ) "] row[" ( NR%5 != 0 ? NR%5 : 5 ) "]"} ' file

# 5  
Old 01-16-2007
MySQL Great logic codes!!!!!!! You the man!

Andrek, Thanks I change this line to make it works!

list=`$list + 1` to list=`expr $list + 1`

and I add

done<datafile

Many thanks!


#!/usr/bin/sh
row=1
list=1
IFS=";"
while read a b c d; do
echo "$a $b $c $d list[$list] row[$row]"
row=`expr $row + 1`
if [ $row -eq 6 ]
then
row=1
list=`$list + 1`
fi
if [ $list -eq 6 ]
then
list=1
fi

_______________________________________________________________
Anbu23, Thanks for the help!

Code:
awk -F";" -v no=1 ' { print $1,$2,$3,$4" list[" ( NR%5 != 0 ? no:no++ ) "] row[" ( NR%5 != 0 ? NR%5 : 5 ) "]"} ' file

I tried the awk too to see if it works. I did not work!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop that will replace the ifcfg data.

Change IP to multiple servers based on a database file. Source ip file is not incremental. # Source host #cat hosts server1 server2 server3 server4 server5 server6 # SOURCE file #cat ip Host IP Netmask GW server1 192.0.0.1 ... (2 Replies)
Discussion started by: invinzin21
2 Replies

2. Shell Programming and Scripting

Compare data with while if else loop ?!

Good midday and hello to everyone! =) If I describe some things wrong please be aware, that english is not my native language!! :D I'm a bit stuck with a script I need to finish. Due to data protection I need to hide some paths but that should be no problem. My script connects to a db2... (1 Reply)
Discussion started by: dsondermann
1 Replies

3. Shell Programming and Scripting

Graphing data with awk/while loop

I need to graph data that's in a file. however, i only know of a way to graph data as it comes (meaning, as it is spat out by whatever process or job). i dont know how to graph data in a file. im using rrdtool to graph data. and i was wondering if anyone know of a way to pass a file (the... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Shell Programming and Scripting

Running awk and while loop on data

so this is what i want to do. i have data that looks like this: DATA: Array: A Interface Type: SAS Unused Space: 0 MB Status: Failed Logical Drive: 2 Size: 279.4 GB Fault Tolerance: RAID 1 Heads: 255 Sectors Per Track: 32... (2 Replies)
Discussion started by: SkySmart
2 Replies

5. Shell Programming and Scripting

Problem in getting data from a loop using grep and cut

The script is following : for each_rec in <file_name> do count=`cut -c -2 ${each_rec} | grep "45"` echo ${count} if ] then amount=`cut -c 24-35 ${each_rec}` echo ${amount} else echo "failed" fi done And the file looks like below : ... (4 Replies)
Discussion started by: mady135
4 Replies

6. Shell Programming and Scripting

loop through file to change some data

Hi, I have a file with the following data -0.00000 0.00000 0.00000 F F F 0.00000 0.00000 0.80000 F F F 0.50000 0.50000 0.60000 F F F 0.50000 0.50000 0.20000 F F F -0.00000 0.00000 0.40000 F F F I would like to change the last 3 lines from F F F to T T T. I tried looping each line but don't... (5 Replies)
Discussion started by: princessotes
5 Replies

7. Shell Programming and Scripting

Get data from two text file in one loop

Hi all I have two file that contain different data.I want to get each file data line by line in one loop.i try with cat but cat works ok against one file File a 23 34 45 File b abc xyz cgh now i want like this in a loop.Below is just example.i am doing some thing smiler.can any... (3 Replies)
Discussion started by: aliahsan81
3 Replies

8. Shell Programming and Scripting

How to combine data files using for loop

Hi, I have 5 files basically;namely file1.txt situated each at folder A to E respectively. I would like to extract out third column from each of these file1.txt from folder A to folder E. Also, I wanted to extract the first and second column which are common. In other words, e.g ... (6 Replies)
Discussion started by: ahjiefreak
6 Replies

9. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies

10. UNIX for Advanced & Expert Users

For loop problem extracting data

I have a problem with my loops. I have a file called users.dat, it has all the users in it. Then I extracted a list of users sending out number of mails with date from Netscape logs. The extracted list (mailuse.dat) has 3 fields: username, number of mails, date. (One user can show up several... (2 Replies)
Discussion started by: nitin
2 Replies
Login or Register to Ask a Question