LinuxQuestions.org
Help answer threads with 0 replies.
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 03-30-2009, 12:19 AM   #1
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Rep: Reputation: 15
Trying to change info into a table like format


I have a file that is called testing1.txt which I created in vi editor in linux. This is what the file looks like:

Mon 24 Week 1
Tue 26
Wed 10
Thu 25
Fri 15
Sat 6
Sun 9

Mon 34 Week 2
Tue 16
Wed 10
Thu 5
Fri 15
Sat 4
Sun 6

Mon 55 Week 3
Tue 3
Wed 11
Thu 5
Fri 3
Sat 4
Sun 8

Is there a way to make it look like this:

Mon 24 34 55
Tue 26 16 3
Wed 10 10 11
Thu 25 5 5
Fri 15 15 3
Sat 6 4 4
Sun 9 6 8

I was hoping to create a command that would take this information and put it into the example above, instead of having them all in their own section (if that makes sense).
 
Old 03-30-2009, 10:02 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,811

Rep: Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190
Quote:
Originally Posted by shorte85 View Post
I have a file that is called testing1.txt which I created in vi editor in linux. This is what the file looks like:

Mon 24 Week 1
Tue 26
Wed 10
Thu 25
Fri 15
Sat 6
Sun 9

Mon 34 Week 2
Tue 16
Wed 10
Thu 5
Fri 15
Sat 4
Sun 6

Mon 55 Week 3
Tue 3
Wed 11
Thu 5
Fri 3
Sat 4
Sun 8

Is there a way to make it look like this:

Mon 24 34 55
Tue 26 16 3
Wed 10 10 11
Thu 25 5 5
Fri 15 15 3
Sat 6 4 4
Sun 9 6 8

I was hoping to create a command that would take this information and put it into the example above, instead of having them all in their own section (if that makes sense).
Makes sense...what have you written so far? What kind of script, in what language??

Post what you've written, and what problem(s) you're having, and we can help.
 
Old 03-31-2009, 12:14 AM   #3
bgoodr
Member
 
Registered: Dec 2006
Location: Oregon
Distribution: RHEL[45] {x86,x86_64}, Debian "testing" {x86,x86_64}
Posts: 219

Rep: Reputation: 36
A Perl implementation

If /tmp/testing1.txt contains your input text which was:

Code:
Mon 24 Week 1
Tue 26
Wed 10
Thu 25
Fri 15
Sat 6
Sun 9

Mon 34 Week 2
Tue 16
Wed 10
Thu 5
Fri 15
Sat 4
Sun 6

Mon 55 Week 3
Tue 3
Wed 11
Thu 5
Fri 3
Sat 4
Sun 8
Then here is a small Perl script that reads that file in and
reformulates the data into what you requested:

Code:
#!/usr/bin/env perl

use FileHandle;
use warnings;
use strict;

my $file = "/tmp/testing1.txt";
my $fileHandle = new FileHandle("<$file");
my $lastWeek = "";
# A reference to a hash keyed by the last week number and
# day of the week, and whose values are those mysterious
# values the OP did not define:
my $data;
die "ERROR: Cannot open $file for reading\n" if not $fileHandle;
while (<$fileHandle>)
{
    chomp;
    my $inLine = $_;
    next if $inLine =~ m/^\s*$/; # skip blank lines
    my $dayOfWeek;
    my $value;
    if ($inLine =~ m/^\s*(\S+)\s+(\d+)\s+Week\s+(\d+)\s*$/)
    {
        $dayOfWeek = $1;
        $value = $2;
        $lastWeek = $3;
    } elsif ($inLine =~ m/^\s*(\S+)\s+(\d+)\s*$/)
    {
        $dayOfWeek = $1;
        $value = $2;
    }
    # If $dayOfWeek is defined, then we matched one of
    # the two patterns above:
    if ($dayOfWeek)
    {
        die "ERROR: Week line not found!\n" if not defined $lastWeek;
        # Store the data into a hash called "%data", to be dumped out at the end of this loop:
        $data->{$lastWeek}->{$dayOfWeek} = $value;
    }
}
$fileHandle->close();
foreach my $dayOfWeek (("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"))
{
    print "$dayOfWeek";
    foreach my $lastWeek (sort(keys(%$data)))
    {
        my $value = $data->{$lastWeek}->{$dayOfWeek};
        print " $value";
    }
    print "\n";
}
bgoodr
 
Old 03-31-2009, 12:34 AM   #4
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
And an awk-solution
Code:
$1 != "" {
days[$1] = days[$1]" "$2
}
END{
  names[1]="Mon"
  names[2]="Tue"
  names[3]="Wed"
  names[4]="Thu"
  names[5]="Fri"
  names[6]="Sat"
  names[7]="Sun"
  for (i=1;i<8;i++){
    print names[i]" "days[names[i]]
  }
}
Run like
awk -f script inputfile


Cheers,
Tink
 
Old 03-31-2009, 01:39 PM   #5
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Hey thanks guys, sorry I had some issues that I wasn't able to view this topic until today. Thanks again for the help. Although I was kind of hoping there was a way to do it without having to resort to creating a script? If that is possible at all. I know there is a join command that can be used somehow, just not entirely sure how to go about doing it though.
 
Old 03-31-2009, 02:23 PM   #6
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
To be able to make use of "join" you'd have to chop the file into
weekly segments, one week per new file .... then you could join
them.

Code:
$ cat weeks                                                                                                                     
Mon 24 Week 1
Tue 26
Wed 10
Thu 25
Fri 15
Sat 6
Sun 9

Mon 34 Week 2
Tue 16
Wed 10
Thu 5
Fri 15
Sat 4
Sun 6

Mon 55 Week 3
Tue 3
Wed 11
Thu 5
Fri 3
Sat 4
Sun 8
$ awk 'BEGIN{out=1}{if($0 !=""){print $1,$2 > "file"out}else{out++}}' weeks                      
$ join file1 file2 > newfile                      
$ join newfile file3                      
Mon 24 34 55
Tue 26 16 3
Wed 10 10 11
Thu 25 5 5
Fri 15 15 3
Sat 6 4 4
Sun 9 6 8
I think the pure awk solution is much prettier ;D ... not to speak
of the hassle of consecutive joins if you have many more weeks.


Cheers,
Tink
 
Old 03-31-2009, 02:31 PM   #7
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Thank you so much, I was thinking there was a awk command in there but wasn't sure how to work it out. The awk command I need to work on some more to understand it much better than I do right now. Thank you so much Tinkster! I appreciate your help.
 
Old 03-31-2009, 02:39 PM   #8
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
Quote:
Originally Posted by shorte85 View Post
Thank you so much, I was thinking there was a awk command in there but wasn't sure how to work it out. The awk command I need to work on some more to understand it much better than I do right now. Thank you so much Tinkster! I appreciate your help.
You're welcome.

Code:
awk 'BEGIN{out=1}{if($0 !=""){print $1,$2 > "file"out}else{out++}}' weeks
I'll try to explain.

Code:
BEGIN{out=1}
Set a variable which will become part of our output
file-name(s) and initialise to 1.

Code:
if($0 !="")
Check whether the line we're working on is blank

Code:
{print $1,$2 > "file"out}
If it isn't, print the first two fields, and redirect
that into a file called file+out (out is our counter of chunks).

Code:
{out++}
Otherwise (we found a separator line) increase the number
out, so the NEXT week goes into a separate file.



Cheers,
Tink

Last edited by Tinkster; 03-31-2009 at 02:40 PM.
 
Old 03-31-2009, 02:44 PM   #9
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Ah, okay. I was going to actually ask, so if I were to add an extra week say week 4. Do I just add it to the weeks file, and then do the whole command situation over again and add a file1 to it? Because as far as I understand it where you have the following:

Code:
$ join file1 file2 > newfile
Is where you are separating the weeks, right? Or am I way off? lol
 
Old 03-31-2009, 03:03 PM   #10
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
Quote:
Originally Posted by shorte85 View Post
Ah, okay. I was going to actually ask, so if I were to add an extra week say week 4. Do I just add it to the weeks file, and then do the whole command situation over again and add a file1 to it? Because as far as I understand it where you have the following:

Code:
$ join file1 file2 > newfile
Is where you are separating the weeks, right? Or am I way off? lol
Way off

The separation happens in the awk bit. The files file1, file2, ...
are the RESULT of the awk, which chops up the weeks-file into
one file per week, so you can then join those. But as I said,
that's becoming unpleasant with larger numbers of weeks, since
join only accepts two files at a time to join.

If you then had a weeks file with e.g. five weeks you'd want to
start thinking about a loop (scripting again!) to get the joins done.

You *really* are better off with my first (pure) awk solution.



Chjeers,
Tink
 
Old 03-31-2009, 03:05 PM   #11
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks, I appreciate it!! I am glad you took the time to explain this to me. I really do appreciate it.
 
  


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
partition table info ufmale Linux - Newbie 3 08-16-2008 07:45 AM
i need rpm output in table format Uday123 Fedora 1 12-17-2005 05:15 PM
alternate partition table format for linux trabant Linux - Software 0 09-21-2005 08:27 AM
Table format with ShellScript paraiso Linux - General 2 05-11-2005 06:39 AM
Wrong partition table info Maik Linux - General 5 01-19-2005 01:42 PM


All times are GMT -5. The time now is 11:33 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