Previous: uniq -u, Up: Examples   [Contents][Index]


4.17 ¶õÇò¹Ô¤ò¤Þ¤È¤á¤ë

ºÇ¸å¤ÎÎã¤È¤·¤Æ¡¤¶õÇò¹Ô¤ò¤Þ¤È¤á¤ëcat -s¤ÈƱ¤¸µ¡Ç½¤ò¼ÂÁõ¤·¤Æ¤¤¤ë¡¤ Ê£»¨¤µ¤È®ÅÙ¤ò¾å¤²¤Æ¤¤¤¯»°¤Ä¤Î¥¹¥¯¥ê¥×¥È¤ò°Ê²¼¤Ë½ñ¤­¤Þ¤¹¡¥

ºÇ½é¤Î¤â¤Î¤Ï¡¤ºÇ½é¤Ë¶õÇò¹Ô¤ò¼è¤êµî¤ê¡¤¤Þ¤À»Ä¤Ã¤Æ¤¤¤ì¤ÐºÇ¸å¤Ë¼è¤êµî¤ê¤Þ ¤¹¡¥

#!/usr/bin/sed -f

# on empty lines, join with next
# Note there is a star in the regexp
:x
/^\n*$/ {
N
bx
}
# now, squeeze all '\n', this can be also done by:
# s/^\(\n\)*/\1/
s/\n*/\
/

°Ê²¼¤Î¤â¤Î¤Ï¤è¤êÊ£»¨¤Ç¡¤ºÇ½é¤Ë¤¹¤Ù¤Æ¤Î¶õ¤Î¹Ô¤ò¼è¤ê½ü¤­¤Þ¤¹¡¥¤Þ¤À»Ä¤Ã¤Æ ¤¤¤ë¾ì¹ç¡¤ºÇ¸å¤Ëñ°ì¤Î¶õÇò¹Ô¤ò¼è¤êµî¤ê¤Þ¤¹¡¥

#!/usr/bin/sed -f

# delete all leading empty lines
1,/^./{
/./!d
}
# on an empty line we remove it and all the following
# empty lines, but one
:x
/./!{
N
s/^\n$//
tx
}

°Ê²¼¤Ï¡¤Á°ÃÖ¤ª¤è¤Ó¸åÃÖ¤µ¤ì¤Æ¤¤¤ë¶õÇò¹Ô¤ò¼è¤ê½ü¤­¤Þ¤¹¡¥¤½¤ì¤ÏºÇ¤â®¤¤¤â ¤Î¤Ç¤¹¡¥sed¤¬¹Ô¤Î½ª¤ê¤Ç¼«Æ°Åª¤Ë¥¹¥¯¥ê¥×¥È¤ÎºÇ½é¤Î¥µ¥¤¥¯¥ë¤ËÌá ¤ë¤È¤¤¤¦»ö¼Â¤òÍøÍѤ¹¤ë¤³¤È¤Ê¤¯¡¤n¤Èb¤òÍѤ¤¤ÆÊ£»¨¤Ê¥ë¡¼¥× ¤ò¼Â¹Ô¤·¤Æ¤¤¤ë¤³¤È¤ËÃí°Õ¤·¤Æ¤¯¤À¤µ¤¤¡¥

#!/usr/bin/sed -nf

# delete all (leading) blanks
/./!d
# get here: so there is a non empty
:x
# print it
p
# get next
n
# got chars? print it again, etc... 
/./bx
# no, don't have chars: got an empty line
:z
# get next, if last line we finish here so no trailing
# empty lines are written
n
# also empty? then ignore it, and get next... this will
# remove ALL empty lines
/./!bz
# all empty lines were deleted/ignored, but we have a non empty.  As
# what we want to do is to squeeze, insert a blank line artificially
i\
bx