Next: , Previous: tac, Up: Examples   [Contents][Index]


4.7 ¹Ô¤ÎÈÖ¹æÉÕ¤±

°Ê²¼¤Î¥¹¥¯¥ê¥×¥È¤Ïcat -n¤ÎÃÖ´¹¤¨¤Ç¤¹¡¥¼ÂºÝ¤½¤ì¤Ï¡¤½ÐÎϤò GNU cat¤Î¤è¤¦¤ËÀµ³Î¤Ë½ñ¼°²½¤·¤Þ¤¹¡¥

¤â¤Á¤í¤ó¡¤¤³¤ì¤ÏÆó¤Ä¤ÎÍýͳ¤«¤éÁ´¤¯°ÕÌ£¤¬¤¢¤ê¤Þ¤»¤ó¡¥¤Þ¤º»Ï¤á¤Ë¾¤Î¤â¤Î ¤ÏC¤Ç¹Ô¤Ê¤¤¤Þ¤¹¡¥ÆóÈÖÌܤ˰ʲ¼¤ÎBourne¥·¥§¥ë¥¹¥¯¥ê¥×¥È¤ÏƱ¤¸ÌÜŪ¤Ç»ÈÍѤµ ¤ì¡¤¤Ï¤ë¤«¤Ë®¤¯¤Ê¤ê¤Þ¤¹¡¥

#! /bin/sh
sed -e "=" $@ | sed -e '
  s/^/      /
  N
  s/^ *\(......\)\n/\1  /
'

¤½¤ì¤Ï¹ÔÈÖ¹æ¤ò½ÐÎϤ¹¤ë¤¿¤á¤Ësed¤ò»ÈÍѤ·¡¤Æó¤Ä¤ÎN¤Ç¹Ô¤ò Æó¤Ä¤Ë¤Þ¤È¤á¤Þ¤¹¡¥¤â¤Á¤í¤ó¡¤¤³¤Î¥¹¥¯¥ê¥×¥È¤Ï°Ê²¼¤ÇÄ󼨤¹¤ë¤â¤Î¤Û¤É¶µ¤ï ¤ë¤â¤Î¤Ï¤¢¤ê¤Þ¤»¤ó¡¥

Áý²Ã¤Ç»ÈÍѤ·¤Æ¤¤¤ë¥¢¥ë¥´¥ê¥º¥à¤òξÊý¤Î¥Ð¥Ã¥Õ¥¡¤Ç»ÈÍѤ·¤Æ¤¤¤ë¤Î¤Ç¡¤¹Ô¤Ï ²Äǽ¤Ê¸Â¤ê®¤¯½ÐÎϤµ¤ì¡¤¤½¤·¤ÆÇË´þ¤µ¤ì¤Þ¤¹¡¥¿ôÃͤÏÊѹ¹¤·¤¿·å¤¬¥Ð¥Ã¥Õ¥¡ ¤ËÆþ¤ê¡¤Êѹ¹¤µ¤ì¤Ê¤¤¤â¤Î¤¬¤â¤¦°ìÊý¤Ë¹Ô¤¯¤è¤¦¤ËʬΥ¤µ¤ì¤Æ¤¤¤Þ¤¹¡¥Êѹ¹¤µ ¤ì¤ë·å¤Ïñ°ì¤Î¥¹¥Æ¥Ã¥×(y¥³¥Þ¥ó¥É¤ò»ÈÍѤ·¤Æ)½¤Àµ¤µ¤ì¤Þ¤¹¡¥¼¡¤Î¹Ô ¤Î¹ÔÈÖ¹æ¤Ï¡¤¼¡¤Î·«¤êÊÖ¤·¤Ç»ÈÍѤµ¤ì¤ë¤è¤¦¤Ë¡¤ºîÀ®¤µ¤ì¥Û¡¼¥ë¥É¶õ´Ö¤ËÊݸ ¤µ¤ì¤Þ¤¹¡¥

#!/usr/bin/sed -nf

# Prime the pump on the first line
x
/^$/ s/^.*$/1/
# Add the correct line number before the pattern
G
h
# Format it and print it
s/^/      /
s/^ *\(......\)\n/\1  /p
# Get the line number from hold space; add a zero
# if we're going to add a digit on the next line
g
s/\n.*$//
/^9*$/ s/^/0/
# separate changing/unchanged digits with an x
s/.9*$/x&/
# keep changing digits in hold space
h
s/^.*x//
y/0123456789/1234567890/
x
# keep unchanged digits in pattern space
s/x.*$//
# compose the new number, remove the newline implicitly added by G
G
s/\n//
h