Introductory Text 001 text text text text text text text text text text text text text text Introductory Text 002 text text text text text text text text text text text text Introductory Text 003 text text text(Those example lines are pretty short, but in the file, some lines are hundreds of characters long.) What I would like to do is reformat it so that it looks like this:
Introductory Text
001 text text text text text text text text text
text text text text text
Introductory Text
002 text text text text text text text text text
text text text
Introductory Text
003 text text text
Specifically, I want to leave the "Introductory Text" lines untouched in all cases, while changing the numbered lines so that they are (at most) 50 characters wide, with no words being split between lines and each new line created having 4 spaces prefixed to it so that it lines up.
#!/usr/bin/ruby
infile = File.new(ARGV[0])
infile.each{ |line|
if ((line=~/Introductory/) || (line=~/^\d+/))
print line
else
print " "
print line
end
}
save it as scriptName.rb, then call it from the command line with
ruby scriptName.rb inputFile.txt >outputFile.txt
perl -pe 'next unless /^\d/; s/(\d.{0,49}) /\1\n/; while (s/\n([^ ].{0,46}) /\n \1\n/) {}; s/\n([^ ])/ \1/'explanation: loop over the file, printing each line once we're done with it. unless it starts with a digit, leave it as is. change (a digit followed by 0 to 49 characters) and a space into that stuff and a newline. then, until there's nothing left to change, change a newline then (a non-space followed by 0 to 46 characters) and a space into a newline, 4 spaces, that stuff, and a newline. this leaves the final word of the original line on a line of it's own, so turn a newline and (a non-space) into a space and that stuff. ugghhh...perl -MText::Wrap -pe 's/^(\d{3}.*)/wrap(""," ",$1)/e'perl -MText::Wrap -pe '$Text::Wrap::columns=50; s/^(\d+.*)/wrap(""," ",$1)/e;'#!/usr/bin/rubyinfile = File.new(ARGV[0])infile.each{ |line| if !(line=~/^\d+/) print line else arr = line.chomp.split(/ /) sum = 0 arr.each{ |word| if (sum + word.length > 50) print "\n " sum = 4 end print "#{word} " sum += (word.length + 1) } print "\n" end}C-5 C-0 M-x set-fill-column], then record a macro [C-x (] that:C-x (
M-x search-forward-regexp ^[0-9] RET
C-a C-SPC
M-F M-F RET space space space space
C-e
M-x fill-region
C-x )
fmt -t -w 50 <> >should be pretty close as well.
perl -pe 'next unless /^\d/; s/(.{0,50}) /\1\n /g'posted by russm at 10:22 PM on April 1, 2008