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.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

This will give you a separate array for each person, with element 0 being the title line and 1-12 being the numbered entries. The IFS needs to be set to newline first though, in order to keep it from breaking at spaces; something I failed to note that in my first post. Or you can just shift the index numbers to account for the extra entry instead if you want.

No need to futz around with IFS when using mapfile, but it seems you need to use -t to strip the newlines from the end of each line.

And thank you for your concern. But I'm half the country away from the quake and wasn't directly affected at all. The strength was only about a 4 here. In fact, I was on my bike at the time heading for a job and didn't notice a thing. I did feel the large aftershock 30 minutes later, however, which set everything rattling again for some time.

I'm sitting at home now watching the aftermath on the news and those tsunami images are scary stuff.

Last edited by David the H.; 03-11-2011 at 08:07 AM.
Reason: As stated

I am a bit confused. Sorry. I am quite new to programming and Bash. Ive had around 2 days worth of experience.

I am fairly average with arrays, and I have not heard of the IFS command..

Also, I maybe did not mention clearly that both the PersonA and PersonB would be in one text file..

Sorry if I have set confusion upon you !

No worries.

IFS is the internal field separator environmental variable. It controls what characters bash views as "word" separators. It's set for space/tab/newline by default. But when you want it to ignore spaces, you can set it to newline only and it will break up the text based on lines instead.

And yes, I realized they're in the same file. That's why I used grep to grab each individual entry. the -A 12 option means that grep will output the matching string you give it, plus the 12 lines following it.

awk can be used in a similar way, but just feed it the values, not the operator, separated by spaces. Then use $1, $2, etc to refer to them.

Code:

echo "${p1array[1]} ${p2array[1]}" | awk '{print $1 + $2}'

Usually awk needs some kind of input from a file or or stdin like this to work on, but another way to do it is to use a BEGIN command block to have it print arbitrary strings without input. The awk language has it's own separate variable system though, so to use it this way you have to first transfer your bash variables into the command using the -v option.

Of course, as grail demonstrated above, awk is a powerful scripting language of its own, and it can do everything you want without needing bash at all. When you have time, I recommend the awk tutorial here.

Finally, you can redirect the output of any of the above directly to a file as before, or you can capture the output of any command into a variable using $(..).