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.
You still haven't mentioned by what means you're trying to
display a "table" ... if the result-set is large, can you
somehow utilise less or something to scroll it?
ok, in the text file it contains customer data. at the moment my code finds that data, puts it into a string then output in another text file and prints it out thats all good. i want that string to be put into a table, just something with columns and headings, not sql, so something like
name number address
joe 1 etc
can't seem to create a table full stop, let alone put the string data into a table, the tutorials make no sense to me and even if I did somehow get it to work there doesn't seem to be a way to give the columns titles. I didn't think that this would be so hard to do (probably isn't I'm just new to linux and not so hot on programming) hope that explains more what I need, I'[m greatful for any help
Seems there are two separate issues here. The first is how to parse a line of data from the file. Given this sample data
Code:
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
and this sample data breakdown under columns
Code:
customer phone number name number of purchases
555555 mr smith 6
555123 mrs smith 9
555011 mr blogg 1
The first column "customer" has no data.
The name is always (really?) two space-separated words, apparently title and surname.
The number of purchases is always the 4th space-separated word.
Data after the 4th space-separated word is discarded.
But the requirement has changed by post 8 and is now
Code:
number name purchase date
555026 mr smith 1 purchase october 2006
Column "purchase date" (wouldn't that be "Purchase Date", in title case?) seems to require data from the 8th and 9th space-separated words but the 7th space-separated word is prefixed (uselessly because it is always purchase?).
A good first step would be to specify exactly the required column headings and how the line is to be parsed to get the data (is it as simple as choosing particular space-separated words?) to go under them.
Bash's read command is very good at parsing space-delimited words from a line of a file as shown in the last example within "Example 15-7" in the ABSG.
The second issue is how to calculate the column widths as the maximum length of either the column title or the data. Given the sample data, this is very easy because
Phone numbers are always longer than "Phone".
Name (=title and surname) are always longer than "Name".
Number of purchases are always shorter than "Number of Purchases".
Bash's printf is at least up to the task. The online examples in ABSG are not ideal for present purposes so here's a give-away for the column title line
Code:
#!/bin/bash
# Column titles
readonly ct_phone='Phone'
readonly ct_name='Name'
readonly ct_no_purchases='Number of Purchases'
# Simmulate getting data from file
phone_no=555123
title='mrs'
surname='smith'
no_purchases=9
# Get lengths
len_phone_no=${#phone_no}
let len_name=${#title}+1+${#surname}
# Write column headings
printf "| %${len_phone_no}s | %${len_name}s | %s |\n" "$ct_phone" "$ct_name" "$ct_no_purchases"
Hi sorry for the late reply guys. Thanks for the advice it has been great I've used awk, however i have a slight problem in that it the file only searches the first input e.g. i put in mr smith 2009 it will have an error, when I type mr smith it is fine, when i type in the year it is fine. It is to do with how the file is set up, but i can't seem to use cut with awk to cut out the other bits of data in order to complete the search. so far i tried awk '{print $5,"\t",$4,"\t",$1," ",$2}' | cut -d: -f2 | but that doesn't work. Any ideas?
I think you need to lay out to us what your expectations are?
Initially you just wanted some data from a file in a specific format, but this has now changed
to wanting to be able to add different search items each time to produce different levels of output.
Would you be able to put down in bullet point notation exactly what you are expecting as both your
possible inputs and outputs?
If however you are happy with awk and just want to keep using queries of your own design, I would point
you to the following web page and say enjoy
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.