Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

1 total

Multi-line pattern matching with sed


# Cf. sed and Multi-Line Search and Replace,
# http://www.ilfilosofo.com/blog/2008/04/26/sed-multi-line-search-and-replace/

echo -n $'hello\nworld\n' | sed -E -n '1h;1!H;${;g;s/hello.*world/HELLO WORLD/g;p;}'

echo -n $'hello\nworld\nhello\nworld\nhello\nworld\n' | sed -E -n '1h;1!H;${;g;s/hello.*world/HELLO WORLD/g;p;}'

echo -n $'hello1\nworld\nhello\nworld\nhello\nworld1\n' | sed -E -n '1h;1!H;${;g;s/hello1(.*)world1/HELLO1\1WORLD1/g;p;}'

echo -n $'hello\nworld\nhello\nworld\nhello\nworld\n' | sed -E -n '1h;1!H;${;g;s/hello.world/HELLO WORLD/g;p;}'


#-------------------


echo -n $'hello\nworld\n' | sed -E -n '/hello/{N; s/\n//; s/hello.*world/HELLO WORLD/g;p;}'

echo -n $'hello\nworld\nhello\nworld\nhello\nworld\n' | sed -E -n '/hello/{N; s/\n//; s/hello.?world/HELLO WORLD/g;p;}'

echo -n $'hello\nworld\nhello\nworld\nhello\nworld\n' | sed -E -n '/hello/{N; s/\n//; s/hello.*world/HELLO WORLD/g;p;}'

echo -n $'hello\nworld\nhello\nworld\nhello\nworld\n' | sed -E  -e :a -e '$!N; s/\n/ /g;s/hello.?world/HELLO WORLD/g; ta'


#-------------------


# See: Sed - An Introduction and Tutorial: Grouping with { and },
# http://www.grymoire.com/Unix/Sed.html#uh-35

# cf. http://codesnippets.joyent.com/posts/show/1732

# list application paths with lsregister
time -p lsregister -dump | sed -E -n -e '/path: .+\.app/,/name:/{ /.+name:.*/d; s/[[:space:]]+path:[[:space:]]+(.+)/\1/; p;}' | nl

# list application names with lsregister
time -p lsregister -dump | sed -E -n -e '/path: .+\.app/,/name:/{ /.+path:.*/d; s/[[:space:]]+name:[[:space:]]+(.+)/\1/; /^[[:space:]]*$/d; p;}' | nl
time -p lsregister -dump | sed -E -n -e '/path: .+\.app/,/name:/{ /.+name:.*/d; s/[[:space:]]+path:[[:space:]]+(.+)/\1/; p;}' | egrep -o '[^\/]+$' | nl

# list version numbers of applications with lsregister
time -p lsregister -dump | sed -E -n -e '/path: .+\.app/,/version:/{ /.+(path|name|identifier):.*/d; s/[[:space:]]+version:[[:space:]]+(.+)/\1/; /^[[:space:]]*$/d; p;}' | nl

1 total