2

所以我发现了很多关于删除两个模式之间的文本打印两个分隔符之间的文本,但我没有找到任何关于使用 bash 函数打印两个模式之间的文本的内容。

如果我有:

"Alas poor Yorik, I knew him well"

我想打印模式“差”和“好”(独家)之间的所有内容,我会得到:

" Yorik, I knew him "

我怎样才能使用 sed 或 awk 之类的东西来实现这一点?

4

1 回答 1

5
dtpwmbp:~ pwadas$ echo "Alas poor Yorik, I knew him well" | sed -e 's/^.*poor //g;s/ well.*$//g'
Yorik, I knew him
dtpwmbp:~ pwadas$ echo "Alas poor Yorik, I knew him well" | awk '{sub(/.*poor /,"");sub(/ well.*/,"");print;}'
Yorik, I knew him

文件输入的用法:

dtpwmbp:~ pwadas$ echo "Alas poor Yorik, I knew him well" > infile
dtpwmbp:~ pwadas$ cat infile 
Alas poor Yorik, I knew him well
dtpwmbp:~ pwadas$ cat infile | sed -e 's/^.*poor //g;s/ well.*$//g'
Yorik, I knew him
dtpwmbp:~ pwadas$ sed -e 's/^.*poor //g;s/ well.*$//g' < infile
Yorik, I knew him
dtpwmbp:~ pwadas$ cat infile | awk '{sub(/.*poor /,"");sub(/ well.*/,"");print;}'
Yorik, I knew him
dtpwmbp:~ pwadas$ awk '{sub(/.*poor /,"");sub(/ well.*/,"");print;}' < infile 
Yorik, I knew him
于 2012-09-15T23:01:15.417 回答