2012-09-15 59 views

回答

4
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 
+1

其實這是你使用,在某些情況下,括號表達式正則表達式的事情(以及*。)或相似的,(*差。)(*)。反向引用 -/2在這個例子中,將更具可讀性,無論如何訣竅是使用正則表達式定義。 –

+0

優秀!謝謝。 –

+0

更新爲文件輸入用法 –