How to make a virtual 2nd column!

All-

Here’s a record formatting question for you:

Say I have a record like in this format…

nnnnnThis is a sample sentence meant to demonstrate desired word wrapping to make the appearance of a virtual column from a single long string that doesn’t have any hard returns.

… and I want to format it to look something like this:

nnnnn This is a sample sentence meant to
demonstrate desired word wrapping
to make the appearance of a virtual
column from a single long string
that doesn’t have any hard returns.

where:

  • nnnnn is a 5 character string.
  • is a tab character (to be replaced by three spaces).
  • The first space after every 35 or so characters in the sentence (2nd field of the record) is replaced by a hard return plus eight spaces.
  • The sample above is one of many records in a file, separated by a hard return.

Thanks again, friends!

-Kurt

Hi –

All-

Here’s a record formatting question for you:

Say I have a record like in this format…

nnnnnThis is a sample sentence meant to demonstrate desired word wrapping to make the appearance of a virtual column from a single long string that doesn’t have any hard returns.

… and I want to format it to look something like this:

nnnnn This is a sample sentence meant to
demonstrate desired word wrapping
to make the appearance of a virtual
column from a single long string
that doesn’t have any hard returns.

where:

  • nnnnn is a 5 character string.
  • is a tab character (to be replaced by three spaces).
  • The first space after every 35 or so characters in the sentence
    (2nd field of the record) is replaced by a hard return plus eight
    spaces.
  • The sample above is one of many records in a file, separated by a
    hard return.

OK, a first shot at it:

Very quick, home-rolled text wrapper (there are others out

there somewhere)

class String
def wrap(cols=80)
gsub!(/\n/," ")
gsub!(/.{1,#{cols}}(?:\s|\z)/) { |s| “#{s}\n” }
end
end

The leading spaces effect

def hanging_indent(tag,text,cols=25)
w = text.wrap(cols).gsub(/^\s*/," " * (tag.size + 3))
w[0…tag.size-1] = tag
w
end

w = “This is a sample sentence meant to demonstrate desired word
wrapping to make the appearance of a virtual column from a single
long string that doesn’t have any hard returns.”

puts hanging_indent(“nnnnn”,w,35)

gives me:

nnnnn This is a sample sentence meant to
demonstrate desired word wrapping
to make the appearance of a virtual
column from a single long string
that doesn’t have any hard returns.

David

···

On Thu, 1 Aug 2002, Kurt Euler wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

I know you already got one response, but here’s another one anyway:

···

All-

Here’s a record formatting question for you:

Say I have a record like in this format…

nnnnnThis is a sample sentence meant to demonstrate desired word wrapping to make the appearance of a virtual column from a single long string that doesn’t have any hard returns.

… and I want to format it to look something like this:

nnnnn This is a sample sentence meant to
demonstrate desired word wrapping
to make the appearance of a virtual
column from a single long string
that doesn’t have any hard returns.

where:

  • nnnnn is a 5 character string.
  • is a tab character (to be replaced by three spaces).
  • The first space after every 35 or so characters in the sentence (2nd
    field of the record) is replaced by a hard return plus eight spaces.
  • The sample above is one of many records in a file, separated by a
    hard return.


#!/usr/bin/env ruby

def print_record(record, width = 35)

get record identifier

id = record.slice! 0…5

remove delimiting whitespace

record.slice! 0…3

split up record into individual words

words = record.split /\s+/

line = id << ’ '
while words.length > 0
# append word to current line
line << ’ ’ << words.shift

# if current line is past the specified width, 
# print it out and start a new line
puts line or line = ' ' * 7 if line.length > width

end

print out last line

puts line
end

process each file passed on the command-line

(ruby 1.7 can do this in a slightly more concise manner

with File::readlines)

ARGV.each { |path|
File.open(path) { |file|
file.each { |line|
print_record(line, 35)
}
}
}

And the sample data:


01234 this is a sample identifier golly look how long this string is hopefully it gets dumped out correctly ladedadeda humdedum.
56789 this is a sample identifier golly look how long this string is hopefully it gets dumped out correctly ladedadeda humdedum. here is an even longer string aslgkjflskdjflksajfl dlska sfdsdf ldfaskjf lkdfjsaljldf salfdkdflskj f fdlkadflf al fdldk jfdkldsfj dsf.

Gives me the following output:

pabs@vault:~/proj/asdf> ./print_record.rb sample.txt
01234 is is a sample identifier golly
look how long this string is
hopefully it gets dumped out
correctly ladedadeda humdedum.

56789 is is a sample identifier golly
look how long this string is
hopefully it gets dumped out
correctly ladedadeda humdedum.
here is an even longer string
aslgkjflskdjflksajfl dlska sfdsdf
ldfaskjf lkdfjsaljldf salfdkdflskj
f fdlkadflf al fdldk jfdkldsfj
dsf.
pabs@vault:~/proj/asdf>

Thanks again, friends!

-Kurt


Paul Duncan pabs@pablotron.org pabs in #gah (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

Hi Kurt,

Here’s a record formatting question for you:

You might want to look into FormatR :wink:
http://www.crhc.uiuc.edu/~rubel/FormatR/

Say I have a record like in this format…

nnnnnThis is a sample sentence meant to demonstrate desired word wrapping to make the appearance of a virtual column from a single long string that doesn’t have any hard returns.

… and I want to format it to look something like this:

nnnnn This is a sample sentence meant to
demonstrate desired word wrapping
to make the appearance of a virtual
column from a single long string
that doesn’t have any hard returns.

where:

  • nnnnn is a 5 character string.
  • is a tab character (to be replaced by three spaces).
  • The first space after every 35 or so characters in the sentence (2nd field of the record) is replaced by a hard return plus eight spaces.
  • The sample above is one of many records in a file, separated by a hard return.

Thanks again, friends!

-Kurt

I have two examples below, one pulls out the number and the other
doesn’t. They both give the same output. Watch out because they will
modify line, you may need a copy if you’ll want the contents later.
Enjoy,
Paul

#!/usr/bin/env ruby

require "format.rb"
include FormatR

#Have the number by itself
template1 = <<DOT
@<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
num, line1
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
line1
DOT

#The number is in the format. As the string is left justified

the extra spaces between the number and the rest of the string get

pushed to the end and aren’t shown.

template2 = []
template2.push (’^<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<’)
template2.push (‘line2, line2’)
template2.push (’~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<’)
template2.push (‘line2’)

f = Format.new (template1);
f2 = Format.new (template2);

num = 12345
line1 = “This is a sample sentence meant to demonstrate desired word wrapping to make the appearance of a virtual column from a single long string that doesn’t have any hard returns.”

line2 = "12345 " + line1

puts "template 1"
f.printFormat (binding)

puts "template 2"
f2.printFormat (binding)

/tmp> ./rt.rb
template 1
12345 This is a sample sentence
meant to demonstrate desired
word wrapping to make the
appearance of a virtual column
from a single long string that
doesn’t have any hard returns.
template 2
12345 This is a sample sentence
meant to demonstrate desired
word wrapping to make the
appearance of a virtual column
from a single long string that
doesn’t have any hard returns.

···

Kurt Euler keuler@portal.com wrote:

Hi –

process each file passed on the command-line

(ruby 1.7 can do this in a slightly more concise manner

with File::readlines)

ARGV.each { |path|
File.open(path) { |file|
file.each { |line|
print_record(line, 35)
}
}
}

File.readlines seems to work OK in 1.6.7:

$ ruby -ve ‘ARGV.each {|f| p File.readlines(f)}’ file1 file2
ruby 1.6.7 (2002-03-01) [i686-linux]
[“file 1 line 1\n”, “file 1 line 2\n”]
[“file 2 line 1\n”, “file 2 line 2\n”]

Most concise would probably be:

ARGF.each {|line| print_record(line,35)}

David

···

On Thu, 1 Aug 2002, Paul Duncan wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav