ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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
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.
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.