例如之間的字符串:巴什 - 提取兩個百分點
((
extract everything here, ignore the rest
))
我知道怎麼不理內的一切,但我不知道該怎麼做相反。基本上,它將是一個文件,它需要提取兩點之間的數據,然後將其輸出到另一個文件。我已經嘗試了無數的方法,並且似乎都告訴我,我指出的縮進在文件中不存在,當它存在時。
如果有人能指引我正確的方向,我會很感激。
例如之間的字符串:巴什 - 提取兩個百分點
((
extract everything here, ignore the rest
))
我知道怎麼不理內的一切,但我不知道該怎麼做相反。基本上,它將是一個文件,它需要提取兩點之間的數據,然後將其輸出到另一個文件。我已經嘗試了無數的方法,並且似乎都告訴我,我指出的縮進在文件中不存在,當它存在時。
如果有人能指引我正確的方向,我會很感激。
文件:
$ cat /tmp/l
((
extract everything here, ignore the rest
someother text
))
腳本
$ awk '$1=="((" {p=1;next} $1=="))" {p=o;next} p' /tmp/l
extract everything here, ignore the rest
someother text
假設要提取裏面的字符串((和)):
VAR="abc((def))ghi"
echo "$VAR"
VAR=${VAR##*((}
VAR=${VAR%%))*}
echo "$VAR"
##
切掉最長的字符串從一開始就; #
從最開始刪除最短的字符串; %%
最後切掉最長的字符串; %
削減而去年底的shortes串
如果您的數據「面向行」,所以標記是單獨(如上例),你可以嘗試以下一些:
function getdata() {
cat - <<EOF
before
((
extract everything here, ignore the rest
someother text
))
after
EOF
}
echo "sed - with two seds"
getdata | sed -n '/((/,/))/p' | sed '1d;$d'
echo "Another sed solution"
getdata | sed -n '1,/((/d; /))/,$d;p'
echo "With GNU sed"
getdata | gsed -n '/((/{:a;n;/))/b;p;ba}'
echo "With perl"
getdata | perl -0777 -pe "s/.*\(\(\s*\\n(.*)?\)\).*/\$1/s"
詩:是的,它看起來像一個舞蹈瘋狂的牙籤
對於許多示例和「瘋狂的太棒」,+1) – cajwine 2013-04-20 21:39:18
sed -n '/^((/,/^))/ { /^((/b; /^))/b; p }'
簡要說明:
/^((/,/^))/: range addressing (inclusive)
{ /^((/b; /^))/b; p }: sequence of 3 commands
1. skip line with ^((
2. skip line with ^))
3. print
爲了排除範圍選擇,需要跳線。
那麼你究竟如何提取它?按行號?字符偏移?或者開始和結束標記? – 2013-04-20 20:50:55