LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-13-2010, 04:34 AM   #16
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928

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?
 
Old 04-13-2010, 04:49 AM   #17
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
1 members found this post helpful.
Old 04-13-2010, 06:06 AM   #18
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
What do you want to use the table for? Is it just for your personal use or for corporate etc? HTML output may be an option?
 
Old 04-13-2010, 06:24 AM   #19
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by j123 View Post
I can't even seem to draw a basic table with results in at the moment, let alone give it columns with titles
Doesn't something like this work for the column titles (headings)?:
Code:
[mherring@Ath ~]$ awk 'BEGIN {printf "%15s%15s%15s\n", "column1", "column2", "column3"}'
        column1        column2        column3
[mherring@Ath ~]$
 
Old 04-13-2010, 06:59 AM   #20
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
  1. The first column "customer" has no data.
  2. The name is always (really?) two space-separated words, apparently title and surname.
  3. The number of purchases is always the 4th space-separated word.
  4. 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
  1. Phone numbers are always longer than "Phone".
  2. Name (=title and surname) are always longer than "Name".
  3. 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"

Last edited by catkin; 04-13-2010 at 07:00 AM.
 
Old 04-13-2010, 07:02 AM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
An alternative as I said using column:

Code:
awk 'BEGIN{print "column1|column2|column3"}' | column -t -s '|'
Delimeter, in this case pipe, '|', can be almost anything you like.
 
Old 04-13-2010, 07:03 AM   #22
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by devnull10 View Post
What do you want to use the table for? Is it just for your personal use or for corporate etc? HTML output may be an option?
it's just for personal use really to keep track of records of customer purchases i don't want to over complicate it with sql or html ideally
 
Old 04-13-2010, 03:43 PM   #23
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by j123 View Post
it's just for personal use really to keep track of records of customer purchases i don't want to over complicate it with sql or html ideally
You haven't responded to some of the other recent replies.......where do things stand? i.e. What is working and what is not?
 
Old 04-14-2010, 11:21 AM   #24
j123
LQ Newbie
 
Registered: Mar 2010
Posts: 12

Original Poster
Rep: Reputation: 0
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?
 
Old 04-14-2010, 01:26 PM   #25
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Mate ... how about you decide on a sensible system? Are the fields TAB
separated? If they are, all you need to do is
Code:
awk -F"\t" '$2 ~ /mr smith/ && $8 ~ /2009/' file
And for crying out loud - if you REALLY want people to help with
your problems, give them the info they're asking for.



Cheers,
Tink
 
Old 04-14-2010, 04:04 PM   #26
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
if you REALLY want people to help with
your problems, give them the info they're asking for.
For example, what about the suggestions on printf for the column headings?
 
Old 04-15-2010, 12:58 AM   #27
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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

http://www.gnu.org/manual/gawk/html_node/index.html#Top
 
  


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:46 AM.

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