Results 1 to 8 of 8
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 ...
- 04-13-2010 #1Just Joined!
- Join Date
- Apr 2010
- Posts
- 6
how to output data from text file 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. My code extracts the data outputs the string to another file which works fine
The text file looks like this
mr smith 1 purchase oct 2007
mrs smith 2 purchase nov 2006
i want it to look like this
name purchase date
blah 1 oct
just trying to draw a table so far has proved impossible. I've looked at tutorials in awk and printf but it just makes no sense to me, other bits are just for sql which is no good. Any help would be great, cheers
- 04-13-2010 #2
Maybe this small tuturial may give you a new start
Awk - Linux Commands
It should be something like this (didn't test it):
$ echo -e "name\tpurchase\tdatel"; \
awk '{print $1," ",$2,"\t",$4,"\t",$5}' file
Luis
- 04-13-2010 #3Just Joined!
- Join Date
- Apr 2010
- Posts
- 6
Luis, you are an absolute star! Thank you so much it worked great, the only problem i have no is some of the columns out of line, can that be resolved by something like %10?
- 04-13-2010 #4
Of course it can be resolved.
The question is how
If you change it to
$ echo -e "date\tpurchase\tname"; \
awk '{print $5,"\t",$4,"\t",$1," ",$2}' file
it will work better
You may ouput a html table (notice an extra setting of OFS to ""), save the output to a file.html, and see it on your browser:
$ echo "<table><tr><th>name</th><th>purchase</th><th>date</th></tr>"; \
awk '{OFS = ""; print "<tr><td>",$1," ",$2,"</td><td>",$4,"</td><td>",$5,"</td></tr>"}' file ; \
echo "</table>";
These are very simple examples. You can do much more with awk.
The GNU Awk User's Guide
Build it command by command and consider putting it all on a shell script after.
Regards
Luis
- 04-13-2010 #5
width
This is a number specifying the desired minimum width of a field. Inserting any number between the ‘%’ sign and the format-control character forces the field to expand to this width. The default way to do this is to pad with spaces on the left. For example:
printf "%4s", "foo"
prints ‘•foo’.
The value of width is a minimum width, not a maximum. If the item value requires more than width characters, it can be as wide as necessary. Thus, the following:
printf "%4s", "foobar"
prints ‘foobar’.
Preceding the width with a minus sign causes the output to be padded with spaces on the right, instead of on the left.
- 04-14-2010 #6Just Joined!
- Join Date
- Apr 2010
- Posts
- 2
To align the columns neatly, try piping the output through "column" it automatically aligns tabbed data.
e.g. ......
user1@Kubuntu:~$ mount
user1@Kubuntu:~$ mount | column -t
Sorry, I tried posting example output, but the proportional fonts used in the forum spoil the alignment.
- 04-14-2010 #7Just Joined!
- Join Date
- Apr 2010
- Posts
- 6
Hi, sorry but I have another problem. When I search, the search only takes in the first input, not the others so for example if i type in Mr Smith 2008 an error comes up, but it works when I type in Mr Smith, or 2008. It is to do with how the file is set up I know, but I've tried using cut function and it doesn't seem to work. This is what I tried awk '{print $5}' | cut -d: -f2 | any ideas?
- 04-14-2010 #8Just Joined!
- Join Date
- Apr 2010
- Posts
- 2
assuming the data is in file called awk_data...
prof green 9 steal jan 1878
mr smith 1 purchase oct 2007
mrs smith 2 purchase nov 2006
miss jones 3 sale sep 2009
master brown 4 rent jul 1987
user1@Kubuntu:~$ awk -F BEGIN { print " Number Name Title Year Month Type"; print " ------ ---- ----- ---- ----- ----"}{print " "$3" "$2" "$1" "$6" "$5" "$4} awk_data | column -t
Number Name Title Year Month Type
------ ---- ----- ---- ----- ----
9 green prof 1878 jan steal
1 smith mr 2007 oct purchase
2 smith mrs 2006 nov purchase
3 jones miss 2009 sep sale
4 brown master 1987 jul rent
again, using a fixed-width font (i.e. in a bash console) the results will line-up.
Try GOOGLE for "awk example"


Reply With Quote