LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-12-2010, 07:14 AM   #1
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Rep: Reputation: 0
how to read data from text file and output into a table?


Hi guys,
I'm having a slight dilemma on reading data from a text file and outputting it into a table then displaying it. Basically I'm writing a shell script that takes information from text files then outputs the data into a table with 4 headings. The extracting of the data is fine, but creating a table i'm having problems with. I think it is possible to do it using the awk function, but so far i'm having a lot of difficulties. Any help would be great. Thanks for your time
 
Old 04-12-2010, 07:28 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
What DB are you using?
Are you going to create table every time?
What are the contents of the data file you are using ?
 
Old 04-12-2010, 07:39 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
What do the text files look like? What do you want the output to look like? What have you tried so far?
 
Old 04-12-2010, 07:40 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
When you say "create a table", I think of something with some borders and formatting---such as you would do in a word processor.

In lower-level tools you can arrange data in columns using tabs. Perhaps that is what you meant.

Prior to creating the table, how is your data arranged?---eg is it in an array?
 
Old 04-12-2010, 08:20 AM   #5
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Original Poster
Rep: Reputation: 0
basically i'm extracting data from a text file and i need to output the data with column headings.
the data in the text file has phone numbers, customer details, number of transactions something like

555555 mr smith 6 purchases last purchase october 2009
555123 mrs smith 9 purchases last purchase september 2009
555011 mr blogg 1 purchase last purchase april 2008

I want to be able to output the data in my shell script so it has headings e.g.

customer phone number name number of purchases
555555 mr smith 6
555123 mrs smith 9
555011 mr blogg 1

I've tried looking for solutions but so far only found ones using sql which is no good for me as I'm only doing it for a shell script using a text file
 
Old 04-12-2010, 08:24 AM   #6
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by j123 View Post
I think it is possible to do it using the awk function, but so far i'm having a lot of difficulties.
Post what you have so far. Don't forget to use CODE tags.
 
Old 04-12-2010, 08:27 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
If the data is set up in the proper order, then perhaps you just need formatting with tabs. Take a look at the "printf" function in AWK. Good AWK tutorial here:
http://www.grymoire.com/Unix/
 
Old 04-12-2010, 04:28 PM   #8
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Original Poster
Rep: Reputation: 0
This is basically the code i have at the moment, simpled down for quickness;

echo 'what do you want to find?'
read input
output = grep $input /home/etc/filename
echo $output

this will bring back;

555026 mr smith 1 purchase october 2006

what i want is to have the output to have column headings

something like

number name purchase date
555026 mr smith 1 purchase october 2006

I've tried adding awk function but so far haven't been able to get it to have a name for the column.
 
Old 04-12-2010, 04:49 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Have you looked at printf? I'm pretty sure it provides for literal text as well as printing variables (AKA "fields" in AWK)
 
Old 04-12-2010, 04:55 PM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Here's a hint:
Code:
[mherring@mystical ~]$ awk 'BEGIN {printf "hello\n"}'
hello
In this case, I did not enter any formatting codes. The grymoire tutorial shows all the codes (which are--I believe--the same as for the printf function in C.)
 
Old 04-12-2010, 06:43 PM   #11
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Original Poster
Rep: Reputation: 0
I found this while looking on that tutorial, but i had errors while trying to implement it into my shell script. can someone give hints on which part creates the headings for the columns?
BEGIN {
format1 ="%10s %6s\n";
format2 ="%10s %6d\n";
printf(format1, "String", "Number");
}
{
printf(format2, $1, $2);
}
 
Old 04-12-2010, 06:49 PM   #12
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 548

Rep: Reputation: 116Reputation: 116
Have you considered using LaTeX? Depending on what you are doing, you might be able to achieve some great results with it. Failing that, perl is also a good candidate.
 
Old 04-12-2010, 10:54 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
Quote:
BEGIN {
format1 ="%10s %6s\n";
format2 ="%10s %6d\n";
printf(format1, "String", "Number");
}
Anything in BEGIN is only ever done once <hint>

I would also throw in that you can have a look at the column function for processing output too.

Last edited by grail; 04-12-2010 at 11:07 PM.
 
Old 04-12-2010, 10:58 PM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by j123 View Post
I found this while looking on that tutorial, but i had errors while trying to implement it into my shell script. can someone give hints on which part creates the headings for the columns?
BEGIN {
format1 ="%10s %6s\n";
format2 ="%10s %6d\n";
printf(format1, "String", "Number");
}
{
printf(format2, $1, $2);
}
If there is a literal string, that's a column heading. If there's a variable,that's data.
Make sense??
 
Old 04-13-2010, 04:48 AM   #15
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Original Poster
Rep: Reputation: 0
ok so with my code, i take the information i need from the text file and that is turned into a string called result which is then cat>result onto screen. would it be easier for me to split up the result somehow, or just try to display it as a whole? The results from the file are always large. I can't even seem to draw a basic table with results in at the moment, let alone give it columns with titles
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script for read user data with emptyLines in a text file and filter them srimal Linux - Newbie 7 11-01-2009 05:37 AM
Shell script to read lines in a text file and filter user data srimal Linux - Newbie 5 10-21-2009 08:41 AM
How to Read data from a Text file in this case ( C++ Programming) smasher Programming 8 12-29-2008 08:55 PM
read the data from the text file which is located in remote machin to oracle databse marthesh Linux - Newbie 1 07-07-2008 08:05 PM
How to read HTML or TXT file and output the data? koolkicks311 Programming 1 04-21-2007 12:13 AM


All times are GMT -5. The time now is 11:43 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration