[ale] sed help
Thomas Stromberg
lists at stromberg.org
Sat Jan 28 00:40:04 EST 2006
On 27 jan 2006, at 23.31, Robert Coggins wrote:
> OK all I hate asking questions like this on here but here we go.
>
> I have a series of files. In each of the files there are many lines
> that
> only have ,,,,, on them. For every line I find in these files I
> want to
> bring the previous line, the ,,,,, line and the next line together.
I'm sure there are shorter solutions, but this was the first answer I
came up with that seemed to work properly. Pop this into a file named
fixme.rb. This assumes you have ruby installed, which Mac OS X and
many Linux distributions seem to.. otherwise, type "yum install ruby"
or "apt-get install ruby" yadda yadda yadda. See the comments for
example syntax.
#!/usr/bin/ruby
# Quick hack to append lines separated by a ,,,, line
#
# format:
# cat /tmp/brokefile.txt | ruby ./fixme.rb > newoutput.txt
data=$stdin.readlines
trail=nil
while line=data.shift
if line
line.chomp!
if line =~ /^,+$/
print line + data.shift
trail=nil
else
if trail
print "\n"
end
print line
trail=1
end
end
end
# if we neglected the previous newline, make one
if trail
puts
end
// Thomas Str?mberg
// http://toadstool.se/
// +1 678 773 9475
More information about the Ale
mailing list