Technology Questions

Go Back   Technology Questions > Software Questions > Operating System Questions > Linux

Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old 09-27-2007, 09:40 AM
Ivan Marsh
Newsgroup Contributor
 
Posts: n/a
A little bash/perl programming help?

Okay, I admit it, I'm stumped.

I'm trying to write a script to parse out text files into 80 charter line
lengths with column numbering over each line and I just can't seem to get
the logic straight in my head. The desired output would look (something)
like:

12345678901234567890123456789012345678901234567890 1234567890
All work and no play makes jack a dull boy. All work and no

123456789012345678901234567
play makes jack a dull boy.

Putting each 80 character section on a separate line with the column
numbers over it.

Anyone have anything that does something like this? I was trying to write
it in bash but got more confused the more I worked on it. A perl example
would work too.

(For the record: No, it's not homework. I'm trying to write a script I
just lost when a very old server died.)

-thx


--
I told you this was going to happen.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

 
Old 09-27-2007, 09:40 AM
  #2 (permalink)  
Old 09-27-2007, 12:10 PM
Michael Trausch
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

Ivan Marsh, on 09/27/2007 12:30 PM said:
> Okay, I admit it, I'm stumped.
>
> I'm trying to write a script to parse out text files into 80 charter line
> lengths with column numbering over each line and I just can't seem to get
> the logic straight in my head. The desired output would look (something)
> like:
>
> 12345678901234567890123456789012345678901234567890 1234567890
> All work and no play makes jack a dull boy. All work and no
>
> 123456789012345678901234567
> play makes jack a dull boy.
>
> Putting each 80 character section on a separate line with the column
> numbers over it.
>
> Anyone have anything that does something like this? I was trying to write
> it in bash but got more confused the more I worked on it. A perl example
> would work too.
>
> (For the record: No, it's not homework. I'm trying to write a script I
> just lost when a very old server died.)
>
> -thx
>
>


I don't know how to help you in perl. However, it's pretty easy in bash
and Python. It can even be wrapped up into a slightly larger Python
program, but I figure it's quicker and easier this way.

See the attached scripts, which work in concert to do the task that I
think you're requesting.

--
Michael B. Trausch http://www.trausch.us/
Pidgin 2.2.0 and plugins for Ubuntu Feisty!
(And Thunderbird 2.0.0.6, too!) http://www.trausch.us/pidgin

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #3 (permalink)  
Old 09-27-2007, 12:30 PM
Robert Newson
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

Ivan Marsh wrote:

> Okay, I admit it, I'm stumped.


....

> 12345678901234567890123456789012345678901234567890 1234567890
> All work and no play makes jack a dull boy. All work and no
>
> 123456789012345678901234567
> play makes jack a dull boy.

....

> Anyone have anything that does something like this? I was trying to write
> it in bash but got more confused the more I worked on it. A perl example
> would work too.


I'm not that experienced as bash programming...ok I've got almost absolutely
none...but as a thought of the logic involved, I'd be writing something
along the lines of:

read line of text
do
print column numbering
print first 80 chars of text
strip top 80 chars off text
while text not null
repeat for all lines of text.

depending upon how string slicing like running off the end of a string, you
may need to add in a length check when printing the first 80 chars;
similarly for stripping off the top 80 chars. Handling EOF is left as an
exercise for the reader.

> (For the record: No, it's not homework. I'm trying to write a script I
> just lost when a very old server died.)


That's the best excuse yet...much better than the dog ate my homework...[no,
I'm training to be a teacher]

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #4 (permalink)  
Old 09-27-2007, 12:40 PM
Mike
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

In article <pan.2007.09.27.16.30.11.967926@you.now>, Ivan Marsh wrote:
> Okay, I admit it, I'm stumped.
>
> I'm trying to write a script to parse out text files into 80 charter line
> lengths with column numbering over each line and I just can't seem to get
> the logic straight in my head. The desired output would look (something)
> like:
>
> 12345678901234567890123456789012345678901234567890 1234567890
> All work and no play makes jack a dull boy. All work and no
>
> 123456789012345678901234567
> play makes jack a dull boy.
>
> Putting each 80 character section on a separate line with the column
> numbers over it.
>
> Anyone have anything that does something like this? I was trying to write
> it in bash but got more confused the more I worked on it. A perl example
> would work too.
>
> (For the record: No, it's not homework. I'm trying to write a script I
> just lost when a very old server died.)
>
> -thx
>
>


Do you want all text filled/formatted/compressed to 80 characters or
do you want each line regardless of line length?

(not tested, just thinking/typing aloud)

#!/usr/bin/perl

# $Id$
# $Log$

use strict;

while(<>) {
s/[\s\r\n]+$//o;
map { print ($_ % 10) } (1 .. scalar($_));
print "\n";
print $_, "\n";
}

--
Posted via a free Usenet account from http://www.teranews.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #5 (permalink)  
Old 09-27-2007, 12:50 PM
Ivan Marsh
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

On Thu, 27 Sep 2007 19:20:34 +0000, Robert Newson wrote:

> Ivan Marsh wrote:
>
>> (For the record: No, it's not homework. I'm trying to write a script I
>> just lost when a very old server died.)

>
> That's the best excuse yet...much better than the dog ate my
> homework...[no, I'm training to be a teacher]


Time ate my RS/6000 F50 (poor thing).

Turns out the script I'm trying to write is even more complicated than I
thought.

It didn't just print 1234567890... it printed:

1 2 3
123456789012345678901234567890...

I know I've seen programs do this before so there must be some fairly
common logic to it, but I can't figure it out.

--
I told you this was going to happen.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #6 (permalink)  
Old 09-27-2007, 01:20 PM
Ivan Marsh
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

On Thu, 27 Sep 2007 18:43:20 +0000, Mike wrote:

> In article <pan.2007.09.27.16.30.11.967926@you.now>, Ivan Marsh wrote:
>> I'm trying to write a script to parse out text files into 80 charter
>> line lengths with column numbering over each line and I just can't seem
>> to get the logic straight in my head. The desired output would look
>> (something) like:
>>
>> 12345678901234567890123456789012345678901234567890 1234567890 All work
>> and no play makes jack a dull boy. All work and no
>>
>> 123456789012345678901234567
>> play makes jack a dull boy.
>>
>> Putting each 80 character section on a separate line with the column
>> numbers over it.

>
> Do you want all text filled/formatted/compressed to 80 characters or do
> you want each line regardless of line length?


The files in question are flat files from database dumps (from different
sources) so theoretically all the lines from the input file should be the
same length.


--
I told you this was going to happen.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #7 (permalink)  
Old 09-27-2007, 01:40 PM
Robert Newson
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

Ivan Marsh wrote:

> On Thu, 27 Sep 2007 19:20:34 +0000, Robert Newson wrote:
>
>
>>Ivan Marsh wrote:
>>
>>
>>>(For the record: No, it's not homework. I'm trying to write a script I
>>>just lost when a very old server died.)
>>>

>>That's the best excuse yet...much better than the dog ate my
>>homework...[no, I'm training to be a teacher]

>
> Time ate my RS/6000 F50 (poor thing).
>
> Turns out the script I'm trying to write is even more complicated than I
> thought.
>
> It didn't just print 1234567890... it printed:
>
> 1 2 3
> 123456789012345678901234567890...
>
> I know I've seen programs do this before so there must be some fairly
> common logic to it, but I can't figure it out.


If you don't want a varying length header, then just print 2 lines, as in:

print 1 2 3
print 123456789012345678901234567890123...

Or am I missing something here?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #8 (permalink)  
Old 09-27-2007, 01:40 PM
Stuart Miller
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?


"Ivan Marsh" <annoyed@you.now> wrote in message
news:pan.2007.09.27.16.30.11.967926@you.now...
> Okay, I admit it, I'm stumped.
>
> I'm trying to write a script to parse out text files into 80 charter line
> lengths with column numbering over each line and I just can't seem to get
> the logic straight in my head. The desired output would look (something)
> like:
>
> 12345678901234567890123456789012345678901234567890 1234567890
> All work and no play makes jack a dull boy. All work and no
>
> 123456789012345678901234567
> play makes jack a dull boy.
>
> Putting each 80 character section on a separate line with the column
> numbers over it.
>
> Anyone have anything that does something like this? I was trying to write
> it in bash but got more confused the more I worked on it. A perl example
> would work too.
>
> (For the record: No, it's not homework. I'm trying to write a script I
> just lost when a very old server died.)
>
> -thx
>
>
> --
> I told you this was going to happen.
>

It looks to me like a perl script for this would be very easy to write.
I would need to know the format of the input text file, delimiters, etc.and
what form the output would take - console, text file, report type file.
Logic would need to check line length, if > 80 take left 80 characters,
process & print; if < 80 and not end of file read & concatenate next line,
and loop.
I would use 2 lines for the column numbers, as
1 2
1234567890123
and find a good monospace font - courier usually works ok

Depending on the complexity of the input and output section, I see it taking
2-3 hours to write.
That means that a professional programmer can do in in 20 minutes....

Stuart




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #9 (permalink)  
Old 09-27-2007, 02:10 PM
Ivan Marsh
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

On Thu, 27 Sep 2007 20:26:11 +0000, Robert Newson wrote:

> Ivan Marsh wrote:
>
>> On Thu, 27 Sep 2007 19:20:34 +0000, Robert Newson wrote:
>>
>>
>>>Ivan Marsh wrote:
>>>
>>>
>>>>(For the record: No, it's not homework. I'm trying to write a script I
>>>>just lost when a very old server died.)
>>>>
>>>That's the best excuse yet...much better than the dog ate my
>>>homework...[no, I'm training to be a teacher]

>>
>> Time ate my RS/6000 F50 (poor thing).
>>
>> Turns out the script I'm trying to write is even more complicated than I
>> thought.
>>
>> It didn't just print 1234567890... it printed:
>>
>> 1 2 3
>> 123456789012345678901234567890...
>>
>> I know I've seen programs do this before so there must be some fairly
>> common logic to it, but I can't figure it out.

>
> If you don't want a varying length header, then just print 2 lines, as in:
>
> print 1 2 3
> print 123456789012345678901234567890123...
>
> Or am I missing something here?


If the file is greater than 80 characters it has to split the lines and
continue counting.

I'm trying to get the data off of the machine at this point just to see
how the script was written. But I have a feeling recovery is a lost
cause.

--
I told you this was going to happen.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #10 (permalink)  
Old 09-27-2007, 02:10 PM
Ivan Marsh
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

On Thu, 27 Sep 2007 20:26:51 +0000, Stuart Miller wrote:


> "Ivan Marsh" <annoyed@you.now> wrote in message
> news:pan.2007.09.27.16.30.11.967926@you.now...
>> Okay, I admit it, I'm stumped.
>>
>> I'm trying to write a script to parse out text files into 80 charter
>> line lengths with column numbering over each line and I just can't seem
>> to get the logic straight in my head. The desired output would look
>> (something) like:
>>
>> 12345678901234567890123456789012345678901234567890 1234567890 All work
>> and no play makes jack a dull boy. All work and no
>>
>> 123456789012345678901234567
>> play makes jack a dull boy.
>>
>> Putting each 80 character section on a separate line with the column
>> numbers over it.
>>
>> Anyone have anything that does something like this? I was trying to
>> write it in bash but got more confused the more I worked on it. A perl
>> example would work too.
>>
>> (For the record: No, it's not homework. I'm trying to write a script I
>> just lost when a very old server died.)
>>

> It looks to me like a perl script for this would be very easy to write.
> I would need to know the format of the input text file, delimiters,
> etc.and what form the output would take - console, text file, report
> type file. Logic would need to check line length, if > 80 take left 80
> characters, process & print; if < 80 and not end of file read &
> concatenate next line, and loop.
>
> Depending on the complexity of the input and output section, I see it
> taking 2-3 hours to write.
> That means that a professional programmer can do in in 20 minutes....


The input files are all fixed length records with the fields falling in
the same place in every record (but not the same in every file):

Last_Name First_Name Address
Smith John 123
Sasafrace Mcgillicutty 7643
Satyanarayan Fred 734673

Etc...






--
I told you this was going to happen.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #11 (permalink)  
Old 09-27-2007, 02:20 PM
Rich Leitner
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

On Thu, 27 Sep 2007 20:26:51 +0000, Stuart Miller wrote:

> "Ivan Marsh" <annoyed@you.now> wrote in message
> news:pan.2007.09.27.16.30.11.967926@you.now...
>> Okay, I admit it, I'm stumped.
>>
>> I'm trying to write a script to parse out text files into 80 charter
>> line lengths with column numbering over each line and I just can't seem
>> to get the logic straight in my head. The desired output would look
>> (something) like:
>>
>> 12345678901234567890123456789012345678901234567890 1234567890 All work
>> and no play makes jack a dull boy. All work and no
>>
>> 123456789012345678901234567
>> play makes jack a dull boy.
>>
>> Putting each 80 character section on a separate line with the column
>> numbers over it.
>>
>> Anyone have anything that does something like this? I was trying to
>> write it in bash but got more confused the more I worked on it. A perl
>> example would work too.
>>
>> (For the record: No, it's not homework. I'm trying to write a script I
>> just lost when a very old server died.)
>>
>> -thx
>>
>>
>> --
>> I told you this was going to happen.
>>

> It looks to me like a perl script for this would be very easy to write.
> I would need to know the format of the input text file, delimiters,
> etc.and what form the output would take - console, text file, report
> type file. Logic would need to check line length, if > 80 take left 80
> characters, process & print; if < 80 and not end of file read &
> concatenate next line, and loop.
> I would use 2 lines for the column numbers, as 1 2
> 1234567890123
> and find a good monospace font - courier usually works ok
>
> Depending on the complexity of the input and output section, I see it
> taking 2-3 hours to write.
> That means that a professional programmer can do in in 20 minutes....
>
> Stuart


Ivan, here's some (modified) code from a vigenere cipher shell script I
wrote. It's got some limitations, specifically the notion that you read
an entire text file into a variable, kind of a no-no in bash scripting
IIRC. I've tested it with text files up to about 15,000 characters on a
PIII, 512MB machine running Fedora 7 and it works, but you may want to
try a "while read line ..." method of reading the lines from the file one
at a time(this didn't work for me in the cipher problem as I couldn't
keep a one to one correspondence with the key text, but should not be a
problem for your application). Failing that, enclosing the $filename in
quotes, I think, will preserve the spaces and newlines from the original
file, ie,

line=`cat "$filename"`

Also, you could create a larger loop to run each "while" statement again
if you want the column numbers atop each line, or just the second "while"
if you just want the lines.

*******Begin Code***********

j=0
k=0
while [ $j -le 80]
do
k=`expr $j % 10` # Resets column header to "0" every 10 characters
printf $k
k=$((k + 1))
j=$((j + 1))
done

echo # prints the 'newline' to start the text below

line=`cat $filename` # Reads the file into variable $line
while [ $i -le 80 ]
do
substring=${line:$i:1}
printf $substring
i=$((i + 1))
done

echo # Another newline, start again.

******End Code***********

Rich ... novice shell scripter, but learning!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #12 (permalink)  
Old 09-27-2007, 03:00 PM
Robert Newson
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

Ivan Marsh wrote:

....
>>If you don't want a varying length header, then just print 2 lines, as in:
>>
>>print 1 2 3
>>print 123456789012345678901234567890123...
>>
>>Or am I missing something here?

>
> If the file is greater than 80 characters it has to split the lines and
> continue counting.


Aha, as in:

9 0 1
123456789012345678901234567890123...

the rest of the line


What happens at 100? An extra row with a one above all subsequent 10s
markers, or just a 1 above the 100 marker?

> I'm trying to get the data off of the machine at this point just to see
> how the script was written. But I have a feeling recovery is a lost
> cause.


good luck

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #13 (permalink)  
Old 09-27-2007, 03:10 PM
Ivan Marsh
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

On Thu, 27 Sep 2007 21:47:38 +0000, Robert Newson wrote:

> Ivan Marsh wrote:
>
> ...
>>>If you don't want a varying length header, then just print 2 lines, as in:
>>>
>>>print 1 2 3
>>>print 123456789012345678901234567890123...
>>>
>>>Or am I missing something here?

>>
>> If the file is greater than 80 characters it has to split the lines and
>> continue counting.

>
> Aha, as in:
>
> 9 0 1
> 123456789012345678901234567890123...
>
> the rest of the line
>
> What happens at 100? An extra row with a one above all subsequent 10s
> markers, or just a 1 above the 100 marker?


That would seem to be the case, but I don't know if the old script did
that.

--
I told you this was going to happen.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #14 (permalink)  
Old 09-27-2007, 08:50 PM
Chris F.A. Johnson
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

On 2007-09-27, Ivan Marsh wrote:
> Okay, I admit it, I'm stumped.
>
> I'm trying to write a script to parse out text files into 80 charter line
> lengths with column numbering over each line and I just can't seem to get
> the logic straight in my head. The desired output would look (something)
> like:
>
> 12345678901234567890123456789012345678901234567890 1234567890
> All work and no play makes jack a dull boy. All work and no
>
> 123456789012345678901234567
> play makes jack a dull boy.
>
> Putting each 80 character section on a separate line with the column
> numbers over it.
>
> Anyone have anything that does something like this? I was trying to write
> it in bash but got more confused the more I worked on it. A perl example
> would work too.
>
> (For the record: No, it's not homework. I'm trying to write a script I
> just lost when a very old server died.)


fold "$FILE" | awk '
BEGIN { n = "1234567890123456789012345678901234567890123456789 0123456789012345678901234567890" }
{ print substr( n, 1, length ); print; print "" }'


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #15 (permalink)  
Old 09-27-2007, 09:00 PM
jellybean stonerfish
Newsgroup Contributor
 
Posts: n/a
Re: A little bash/perl programming help?

On Thu, 27 Sep 2007 21:47:38 +0000, Robert Newson wrote:

> Aha, as in:
>
> 9 0 1
> 123456789012345678901234567890123...
>
> the rest of the line
>
>
> What happens at 100? An extra row with a one above all subsequent 10s
> markers, or just a 1 above the 100 marker?


I think it would look cleaner like this...

90 100 110
1234567890123456789012345678901234567890
the rest of the line


stonerfish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
installing server, interpreter and perl in vista. db Windows Vista 1 09-18-2007 11:00 PM
Printing: OpenOffice yes, bash no. toni Linux 6 06-27-2007 05:30 PM
help! - /bin/bash Permission Denied Dokurobei Apple Macintosh Hardware 1 05-06-2007 07:18 AM
grep in a bash script hobbzilla Linux 4 05-06-2007 12:11 AM
Are you an experienced Perl, PHP, MySQL, etc. Developer? Michael Desktop Computers 0 02-06-2007 05:57 PM


New To Technology Questions? Do You Need Help with Your Computer or Device? Do You Need Help with this site?

All times are GMT -8. The time now is 03:44 PM.


2003 - 2009 All Rights Reserved. Technology Questions

Search Engine Friendly URLs by vBSEO 3.3.0