Re: A little bash/perl programming help?



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

"Ivan Marsh" <annoyed@xxxxxxx> wrote in message
news:pan.2007.09.27.16.30.11.967926@xxxxxxxxxx
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:

123456789012345678901234567890123456789012345678901234567890 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!

.



Relevant Pages

  • Re: pop-forum Re: poplog-dev A look at Poplog on the Macintosh
    ... with the poplink command. ... When trying to make pglink produce a 'sh' rather than a 'csh' shell ... script for linking and when trying to make it put unexpanded environment ... using the shell script pglibr, ...
    (comp.lang.pop)
  • Files appear to be cross linked
    ... script requires a number of standard environment variables to be set ... so it calls a second shell script to set these at the session level, ... control should return to the first shell script, ... that the script executes to the last line prior to the displaying of ...
    (comp.unix.solaris)
  • Re: [PHP] Catch STDERR
    ... into your PHP script? ... Or do you just need STDOUT in one variable, and STDERR in another, ... Another option is to try to write a .sh shell script to get what you ...
    (php.general)
  • Re: basic questions
    ... Setting a script that runs at 3:00 ... Set chron to run that shell script. ... Debug the GUI application with the REPL. ... You start up the Web browser, then browse the Web, visit a hundred ...
    (comp.lang.lisp)
  • Re: Converting large bash script to TCL
    ... I know that TCL and shell scripting share a lot of similarities. ... The shell script ... Determine what it (the script) is supposed to do ... echo "WOULD YOU LIKE TO INSTALL NAC DRIVER ...
    (comp.lang.tcl)