LinuxQuestions.org
Visit Jeremy's Blog.
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, 05:34 AM   #16
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910

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, 05: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, 07:06 AM   #18
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 548

Rep: Reputation: 116Reputation: 116
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, 07:24 AM   #19
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 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, 07:59 AM   #20
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
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 08:00 AM.
 
Old 04-13-2010, 08:02 AM   #21
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
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, 08: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, 04:43 PM   #23
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
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, 12:21 PM   #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, 02:26 PM   #25
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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, 05:04 PM   #26
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:
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, 01:58 AM   #27
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
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


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 12:09 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