问题1:假设文本中music与realist成对出现,替换所有的realist与music所在行.
解决方法1:
sed -nr '/music/{h;ba;};/realist/{h;bb;};p;d;
:a n;/realist/{p;g;s/([^/n]*)/n(.*)//2/n/1/;p;be;};H;ba;
:b n;/music/{p;g;s/([^/n]*)/n(.*)//2/n/1/;p;be;};H;bb;
:e n;p;be'
解决方法2:
sed -nr '1h;1!H;${
x;s/([^/n]*music[^/n]*)/n(.*)([^/n]*realist[^/n]*)//3/n/2/1/;te
s/([^/n]*realist[^/n]*)/n(.*)([^/n]*music[^/n]*)//3/n/2/1/;
:e p;q;}'
以上都来自http://bbs.chinaunix.net, cjaizss
问题2
sed脚本
#!/bin/sed -f
/music/{=;q;p}
/realist/{=;p}
d
源文件
......
......music......
......
......realist......
......
......music......
......
......realist......
......
想要实现只打印第一次出现music的行及其所在的行号,realist出现的的所有行及其行号。
解决方法:
Define each state as
a: music outputted, realist never
b: realist outputted, music never
c: music outputted, realist outputted
s: music never, realist never
and from the FSM combination analysis, we can combine state a and state c into new state a as the second below diagram.
经典范例
#! /bin/sed -f
#
# turing.sed -- emulate a Turing machine
#
# Christophe Blaess <[email]ccb@club-internet.fr[/email]>
# [url]http://perso.club-internet.fr/ccb/[/url]
# See text file for information about Turing Machine script.
# Read all the instructions, and add a final newline.
:read
N
$!b read
G
# Delete comments extending from a '#' to the end of the line.
s/#[^/n]*/n//g
s/#.*$//g
# Use a colon to separate the instructions.
s//n/:/g
# Is there an initial tape ?
/|/ s//(.*/)|/([^:]/)/([^:]*/):/(.*/)/|/2|/3:/1/4/
# else insert a blank one.
/|/!s//(.*/)/| |:/1/
# Reserve the storage place at the beginning of the pattern space,
# then set the current state to zero.
s//(.*/)/0x/1/
# Start the machine !
:loop
# Display only the tape and the state.
h
# (comment out the next two lines to see internal data when
# debuging TM script)
s/:.*//
s/^/(./)./(/1) /
p
g
# Check the content of the current cell.
/|[^:|]|/!{
s/.*/Internal error in the Turing machine/
q
}
# Store in second position the symbol read on the tape
s/^/(./)./(.*/)|/(./)|/(.*/)//1/3/2|/3|/4/
# Have we reached a final state ?
/^/(./).*:/1/!{
s//(./).*/Final state /1 reached... end of processing/
q
}
# Is there an instruction for this state and this cell content ?
/^/(../).*:/1/!{
s/^/(./)/(./).*/No instruction for state /1 and cell /2/
q
}
# Look for the new content to write.
/^/(../).*:/1[^:|]/!{
s/.*/I can't write this symbol on the tape!/
q
}
s/^/(../)/(.*/)|.|/(.*/):/1/(./)//1/2|/4|/3:/1/4/
# Look for the direction of movement.
/^/(../).*:/1.[ LRlr]/!{
s/.*/Movement must be specified as L, R or space/
q
}
# Clear the substitute flag that we will use later.
t nop
:nop
/^/(../).*:/1. / {
# Direction = ' ' -> Don't move the head
b end_move
}
/^/(../).*:/1.[Ll]/ {
# Move the head to the left if the tape is long enough,
s/^/(../)/(.*/)/(./)|/(./)|//1/2|/3|/4/
t end_move
# else extend the tape with an empty cell.
s/|/(./)|/| |/1/
b end_move
}
# Move the head to the right, if the tape is long enough,
s/|/(./)|/([^:]/)//1|/2|/
t end_move
# else extend the tape with an empty cell.
s/|/(./)|//1| |/
:end_move
# Check the new state,
/^/(../).*:/1..[^:|]/!{
http://bbs2.chinaunix.net/viewthread.php?tid=1258330&extra=&page=1 s/.*/I can't use this symbol as new state/
q
}
# then switch the machine state
s/^/(./)/(./)/(.*/):/1/2/(../)/(./)//5/2/3:/1/2/4/5/
# Garbage collector : cut the blank portions of the tape,
# on the left,
s//(../) *//1/
# then on the right.
s//(../)/([^:]/) *://1/2:/
b loop
###### end of the script
参考网址:
http://en.literateprograms.org/Turing_machine_simulator_(Sed)?printable=yes
http://bbs2.chinaunix.net/viewthread.php?tid=1258330&extra=&page=1
http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2008-05/msg00430.html
尤其感谢cjaizss.