1
I have a question that involves the bash scripting language.
I have multiple directories
/studentName
/studentMail
/studentNumber
In each of these directories is a file name.txt
, mail.txt
, number.txt
.
Now I need to create a function that will do the same as the SELECT
function of a MySQL database. It doesn't need to read a single line. Just display all contents of those 3 files and sort them. which means i need something like this as output.
studentname | studentmail | studentnumber
I came up with 2 ways.
first:
cat /studentName/name.txt /studentMail/mail.txt /studentNumber/number.txt > summary
cat summary
This will display all contents of the 3 files under each other, which is obviously not good.
I also came up with this:
paste /studentName/name.txt /studentMail/mail.txt /studentNumber/number.txt
This does display all contents but still not really sorted. And I also later on need to be able to only select 1 row to be displayed.
Can anybody help me do this?
PS: I know about sort
, but then all contents get displayed under each other, somehow I am not doing it right?
When I use the paste option, i get this as output:
bryan 912391923 bryan@bryan
hello 2030123 lalal@lallw
ollo 23123123 ollo@ollo
What i want is that they will be displayed in a table like option. Where the name gets displayed in a first column and the number in a second etc. – bryan – 2011-06-01T20:12:23.213
@bryan: It would help to know the contents and/or format of those files and the results you expect. Otherwise, I'm just taking stabs in the dark. To me, the paste option output above looks pretty good. It has the name, id and email of three students. Is each of those records a single line? (SU Comments don't seem to support newlines.) – garyjohn – 2011-06-01T20:17:50.790
Well, the content of the following files is pretty simpel,
*name.txt
has in it's file for expample:
bryan eeden hello
the file number.txt has in its file as example: 1234567 34567 34688
and mail.txt has for example:
bryan@bryan hello@hello test@test
The paste option indeed puts these things next to each other, but i need to increase the tabs in the output since sometimes there is no tab between the 3 colums – bryan – 2011-06-01T20:33:39.753
@bryan: So it sounds like
paste
basically works but that the output format isn't quite right. OK. One solution to that is to pipe the results into theexpand
command with a tab setting large enough to space the output as you want. For example,| expand -t20
will replace each tab by enough spaces to align the next word at the next column that's a multiple of 20 spaces. You could also format the output usingawk
. It gives good results but is more work. – garyjohn – 2011-06-01T20:57:18.590Wauw cool thanks garyjohn. The expand indeed did the trick. I have been told to try to use awk indeed. But I have no idea on how to use it, i cannot quite understand the man page of awk, maybe you can help me on the good way on how to use awk ( if you know how it works ). – bryan – 2011-06-01T21:04:31.550
@bryan: I updated the answer to include a brief
awk
example. – garyjohn – 2011-06-01T22:15:19.913