LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2007, 03:50 PM   #1
meatwad69sofine
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Rep: Reputation: 0
while loop help???


Hello

I'm trying to write a while loop that will do the following.

read a file line by line for example /etc/hosts, 100 times, so if the file is only 6 lines long, it will echo the lines and on the 7th line it will re read the file from the beginning. and echo what number its on + the line number from the file...

so if /etc/hosts looks like this it should output the following

1 127.0.0.1 localhost 1
2 192.168.0.2 server1 2
3 192.168.0.3 server2 3
4 192.168.0.4 server3 4
5 192.168.0.5 server4 5
6 192.168.0.6 server5 6
1 127.0.0.1 localhost 7
2 192.168.0.2 server1 8
3 192.168.0.3 server2 9
4 192.168.0.4 server3 10
5 192.168.0.5 server4 11
6 192.168.0.6 server5 12
etc
etc
etc

please help, i'm really green
 
Old 11-26-2007, 03:57 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
One very simplified way:
Code:
let i=0
while [[ $i -lt 100 ]] 
do
    cat -n /etc/hosts
    let i=$i+1
done
 
Old 11-26-2007, 04:30 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by jim mcnamara View Post
One very simplified way:
Code:
let i=0
while [[ $i -lt 100 ]] 
do
    cat -n /etc/hosts
    let i=$i+1
done
That reads the whole file 100 times--I think he wants to have 100 line reads--cycling thru the file as many times as necessary.

I think maybe just putting "read" in the loop--instead of cat--might do it.

Note; For read to get input from the file, you need:
exec 0<filename
 
Old 11-26-2007, 09:46 PM   #4
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Meatwad may be green, but I'm sure he recognises that if a file is only six lines long, and he wants to print the file contents to cover 100 lines, that it is only necessary to actually read the file once
Code:
awk '{ x[NR]=$0; next }
 END { for (i==0; i<100;i++)
       printf "%-3d %-3d %s\n", (i+1), ((i%NR)+1), x[((i%NR)+1)];
}' /etc/hosts
If I'm a bit heavy on the () in the printf, it's because I have had one or two bad experiences in not forcing expression evaluation from time to time. It's never been obvious until I found the problem. now I tend to avoid it.
The file is read into array x, controlled by the record counter. When the file is finished we loop around from 0 to 99, this is because otherwise the modulus returns a 0 when hitting the number of rows in the file, so I add 1 to convert say 0 to 22 to correctly read 1 to 23. I also need to start the counter i at 0 otherwise my modulus with my adjustment will start at 2 and I'll be unhappy.
The printf statement merely makes the output slightly neater. Modulus and proper offsets are neat, I like them.
Quote:
chmod 755 test
./test
Quote:
1 1 #
2 2 # hosts This file describes a number of hostname-to-address
3 3 # mappings for the TCP/IP subsystem. It is mostly
. . .
22 22 ff02::3 ipv6-allhosts
23 23 127.0.0.2 C4SL101D.site C4SL101D
24 1 #
25 2 # hosts This file describes a number of hostname-to-address
. . .
98 6 # "named" name server.
99 7 # Syntax:
100 8 #
It may not be the most concise implementation, but there is no 'think' about it, as witnessed by the fragments of the output.
 
Old 11-28-2007, 01:11 PM   #5
meatwad69sofine
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Original Poster
Rep: Reputation: 0
hmmm, I couldn't get the ^ awk item to work, I created the below, which works but doesn't reread the file from the beginning and do so until 100 is completed. Any help?

#!/bin/bash
min=0
max=100
if [ $min -lt $max ]; then
for i in `cat /etc/hosts | egrep -v "^#" | awk '{print $2}'`; do
echo "$i $min"
let min=$min+1
done
fi

output
###########
localhost 0
loghost 1
jetfire 2
server1 3
server2 4
server3 5
 
Old 11-28-2007, 07:15 PM   #6
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Hi Meatwad,
I take it that you cut and pasted my code rather than re-typing it?

I normally place my awk within a bash/bourne script. I have changed that so that there should be no problems with your system if you do anything that I didn't anticipate. I have also changed the position of the 1-linesinfilecount and the 1 to 100 count to better match your original spec.

Code:
#!/bin/sh
/usr/bin/awk '{ x[NR]=$0; next }
 END { for (i==0; i<100;i++)
       printf "%-3d %50s %-3d\n", ((i%NR)+1), x[((i%NR)+1)], (i+1);
}' /etc/hosts
If you have problems, what distro are you using (not using Solaris by chance?), and the version of your awk. This can be reported by typing:
awk --version

I regret I can't help you with your coded version, that would be too much like eating my own feet. You can change the 50 in the printf statement if you want less room for the lines in your file.

More importantly, if it doesn't run properly, what errors do you get.
Cut and paste them within code tags so that I can see please. Otherwise i'm just left guessing.

I'm truly sorry that you couldn't get my code to work, but you don't say exactly how it failed for you, so it is a tad difficult to help positively. You saw an extract of the results that I obtained, so the code isn't just a guess, it was tested and works. So don't dismiss it, let's solve the problem, not just write a different bit of code for the sake of it. I do respect that it is your call though.

Regards,

PS place [ code ] [ /code ] tags (no spaces) around your code to preserve indents etc or you will earn the wrath of the moderators. It makes your code and anything else that needs formatting preserved much easier to read.

Last edited by PAix; 11-28-2007 at 07:18 PM.
 
Old 11-30-2007, 11:55 AM   #7
meatwad69sofine
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for your help.. This is infact for solaris, awk won't display the version id, it just hangs with the --version argument.

SunOS jetfire 5.10 Generic_127111-03 sun4u sparc SUNW,Sun-Blade-100

-bash-3.00$ ./script7.awk
awk: syntax error near line 2
awk: illegal statement near line 2

I', assuming lnx and sol use different awk bundles and some syntax might not carry between...
 
Old 11-30-2007, 05:02 PM   #8
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Hi Meatwad,

Ok, that's fine I think. I refered to Solaris because traditionally (my experience is dated) Solaris has both awk and nawk installed. In Solaris/SunOS terms, awk is a pre-1989 implementation of the language that has very little in the way of floating point arithmetic or a lot of other developments since 1989. Nawk (New Awk) is the accepted standard product to use on Solaris and doesn't have the later developments that came with GNU Awk, sometimes called Gawk and other times just refered to as awk.
If you type this at the command line and it errors then it's calling the earlier awk program and is not of ANY use to you. If it returns 0.5 as the answer then you are on the money and I'm foxed.
Code:
awk ' BEGIN { print 1/2 } '
The code in my examples is, I believe, completely nawk compatible and uses no extensions that have a life only in gawk. If you change /usr/bin/awk to /usr/bin/nawk or possibly /bin/nawk (really you will have to ascertain the correct path yourself) then I feel confident that my example should work. The second example is nearer to your requirement.

Quote:
Perhaps a Solaris user out there could independently confirm that the code is good under nawk?

This is a functional test request for Bigears, can you assist? I thought about the Bat Phone and the big searchlight, but they don't seem to be working tonight Billy! (Ian)
Regards,

Last edited by PAix; 11-30-2007 at 05:09 PM.
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM
For Loop Help Haloony Linux - Newbie 9 12-28-2006 10:24 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:15 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