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 04-17-2007, 11:54 PM   #1
IIcucucoolio
LQ Newbie
 
Registered: Apr 2007
Posts: 2

Rep: Reputation: 0
C++ Modify a txt file!


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.

vaagaard
Aagaard, Virginia

jwoody3
Aaron, Gloria

maaron1
Aaron, Michael

mea1328
Aarons, Marissa

jabakoya
Abakoyas, Jiffar

aabalos
Abalos, Ashley

lxa1940
Abarca, Lindsay


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?
 
Old 04-18-2007, 01:07 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
Code:
sed -e '1~2s/^.*/&@book-back.com/' -e '2~2s/.*/"&"/' testfile
vaagaard@book-back.com
"Aagaard, Virginia"
jwoody3@book-back.com
"Aaron, Gloria"
maaron1@book-back.com
"Aaron, Michael"
mea1328@book-back.com
"Aarons, Marissa"
jabakoya@book-back.com
"Abakoyas, Jiffar"
aabalos@book-back.com
"Abalos, Ashley"
lxa1940@book-back.com
"Abarca, Lindsay"
This line will produce two files, one with usernames and the other with realnames:
Code:
sed -n -e '1~2s/^.*/&@book-back.com/w bookback-usernames' -e '2~2s/.*/"&"/w bookback-realnames' testfile
> cat bookback-usernames
vaagaard@book-back.com
jwoody3@book-back.com
maaron1@book-back.com
mea1328@book-back.com
jabakoya@book-back.com
aabalos@book-back.com
lxa1940@book-back.com
> cat bookback-realnames
"Aagaard, Virginia"
"Aaron, Gloria"
"Aaron, Michael"
"Aarons, Marissa"
"Abakoyas, Jiffar"
"Abalos, Ashley"
"Abarca, Lindsay"
 
Old 04-18-2007, 05:41 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 04-18-2007, 06:37 PM   #4
IIcucucoolio
LQ Newbie
 
Registered: Apr 2007
Posts: 2

Original Poster
Rep: Reputation: 0
how would I acutually go about running this solution in Perl. I read the code and understand it but I am not sure how to run it.
 
Old 04-18-2007, 06:48 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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, "<".
Code:
$ chmod 755 test.pl
$ ./test.pl < mydata.txt
 
Old 04-25-2007, 10:51 AM   #6
uNoWho
LQ Newbie
 
Registered: Apr 2007
Posts: 1

Rep: Reputation: 0
[QUOTE=IIcucucoolio]Hello! My name is Anthony...

vaagaard
Aagaard, Virginia

jwoody3
Aaron, Gloria

maaron1
Aaron, Michael

mea1328
Aarons, Marissa

jabakoya
Abakoyas, Jiffar

aabalos
Abalos, Ashley

lxa1940
Abarca, Lindsay


Ant,
If you're going to spam KSU students, don't post their names in a public discussion forum. The schoool frowns upon that.
-A friend
 
  


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 can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
reading a txt file vidyashankara Linux - Software 2 06-22-2006 06:52 PM
c++ writing to a .txt file xemous Programming 8 09-01-2004 10:57 PM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 08:33 AM
modify file access & modify timestamps i2itstud Linux - General 1 05-20-2003 03:34 AM

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

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