TOP Linux /usr/bin

Command Line option

  • -n, --quiet, --silent : suppress automatic printing of pattern space
  • -e script, --expression=script : add the script to the commands to be executed
  • -f script-file, --file=script-file : add the contents of script-file to the commands to be executed
  • i[SUFFIX], --in-place[=SUFFIX] : edit files in place (makes backup if extension supplied)
  • r, --regexp-extended : use extended regular expressions in the script.

Regular Expression

&ananme(regex)
Regular Expression

samples

$ echo oneone | sed 's/^\(.*\)\1$/twotwo/'
twotwo
$ wsdkinc LARGE_INTEGER | sed 's/\([^:]\+\):.*/\1/'
/cygdrive/c/Program Files/Microsoft SDKs/Windows/v6.0/Include/AqAdmTyp.h
:

Pattern Space, Hold Space

h H    Copy/append pattern space to hold space.
g G    Copy/append hold space to pattern space.
x      Exchange the contents of the hold and pattern spaces.
n N    Read/append the next line of input into the pattern space.
d      Delete pattern space.  Start next cycle.
D      Delete  up  to  the first embedded newline in the pattern space.
       Start next cycle, but skip reading from the input  if  there  is
       still data in the pattern space.
p      Print the current pattern space.
P      Print up to the first embedded newline of  the  current  pattern
       space.
s/regexp/replacement/ 
       Attempt  to match regexp against the pattern space.
w filename
       Write the current pattern space to filename.
W filename
       Write the first line of the current pattern space  to  filename.
       This is a GNU extension.

test

all: test

dummy:
	@echo creating dummy file
	for x in `seq 1 10` ; do echo $$x ; done > dummy.txt
	cat dummy.txt

test:
	@echo testing sed pattern buffers
	make dummy 
	#make print_even_lines
	#make print_odd_lines
	#make print_every_3_lines
	#make aho
	#make replace_odd_even
	#make insert_new_mod_line
	make print_line_number


print_even_lines:
	sed '1d;n;d' < dummy.txt  # print even lines
	# n  .. get next line to pattern buffer
	# d  .. delete pattern buffer

print_odd_lines:
	sed 'n;d' < dummy.txt     # print odd lines

print_every_3_lines:
	sed '1d;2d;n;N;d' < dummy.txt     # print every 3 lines
	# N  .. append next line to pattern buffer

aho:
	sed 'n;n;s/.*/aho/' < dummy.txt     # print every 3lines

replace_odd_even:
	sed -n 'h;n;p;g;p' < dummy.txt         # replace odd even lines	
	# h  .. copy pattern space into hold space(overwrite)
	# p  .. print pattern space.  notice the -n option in command line.
	# g  .. copy hold space into pattern space(overwrite)
	
insert_new_mod_line:	
	sed '/7/{h;s/7/7.5/g;x;G}' < dummy.txt # insert new mod line
	# x  .. swap pattern and hold spaces.
	# G  .. append hold space to pattern space
	

print_line_number:
	sed '=' < dummy.txt         # print current line number

管理人/副管理人のみ編集できます