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.
Hello! My name is Anthony. I am kind of new to programming in C++, and prgramming in general. I am trying to write a program that seems very simple, but i just can't figure it out. Here's what I need to do.
I have to edit and organize this txt file that I recieved. First off, the list when opened up in Word is 650 pages long. Its a huge project. Here is what I have to do. I have two pieces of information that belong together. The first part of the informaiton is the username. Right below the username is the person's actual name. I have placed the pieces of information that go together in the same color.
The first line of information is the username. I need to add @book-back.com to the end of each of the usernames. ex: vaagaard@book-back.com (the website www,book-back.com is my website). I would like to store all of the email addresses in one txt file from top to bottom( it is already in alphbetical order). I would like to store the second part in another txt file from top to bottom. (Really, I would like to have both in an Excell file with the User names in one colunm and the email addresses in another colunm beside the correct name. I have no idea how to do this. Could anyone please lead me in the right direction?
Do you need to always do this in a program you are writing, or is this just a one time thing? Why the C++ reference in the title?
How consistent is the input? Is every odd line a user name and every even line a real name? A sed or awk script might be better suited for the task.
For example, here is a one-liner that will add @book-back.com to every odd line and quote the even lines.
While you could certainly write a C++ program for doing this, I'd use something else. I feel that Perl is ideal. It even has a module for writing excel spread sheets. On Ubuntu this is provided by the package libspreadsheet-writeexcel-perl. I would imagine there is a SuSE package too, but I don't know what it is called (do post that here if/when you find it).
Here's a Perl solution which writes a file called users.xls:
Code:
#!/usr/bin/perl -w
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("users.xls");
my $worksheet = $workbook->add_worksheet();
my $row = 0;
$worksheet->write($row, 0, "Username");
$worksheet->write($row, 1, "Real Name");
$worksheet->write($row, 2, "email address");
my ($this_user);
while(<>)
{
chomp; # get rid of \n at end of line
if ( $. % 2 != 0 ) # true on lines with odd numbers
{
$this_user = $_;
}
else
{
$row++;
$worksheet->write($row, 0, $this_user);
$worksheet->write($row, 1, $_);
$worksheet->write($row, 2, $this_user.'@book-back.com');
}
}
Of course, you can do a lot more to pretty up the results - the Spreadsheet::WriteExcel module supports various formatting options. I just provided a bare bones script to give you an idea.
Just save it into a file, for example with the name "test.pl". Then make it executable, and then call it. If you didn't save the fille somewhere which is included in the PATH variable, you need to explicitly state it's path. The <> operator in perl reads from STDIN, or if provided any files mentioned on the command line. Thus you can either pipe input into it, or specify the input file as an argument to the program. In this example, I pipe input into it with the input re-direction operator, "<".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.