how to read data from text file and output into a table?
Linux - NewbieThis 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
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.
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
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
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/
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.)
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);
}
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.
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??
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.