1

For example:

((

extract everything here, ignore the rest

))

I know how to ignore everything within, but I don't know how to do the opposite. Basically, it'll be a file and it needs to extract the data between the two points and then output it to another file. I've tried countless approaches, and all seem to tell me the indentation I'm stating doesn't exist in the file, when it does.

If somebody could point me in the right direction, I'd be grateful.

4

4 回答 4

2

如果您的数据是“面向线的”,因此标记是单独的(如示例中所示),您可以尝试以下一些方法:

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"

ps:是的,它看起来像疯狂牙签的舞蹈

于 2013-04-20T21:19:01.797 回答
1

Assuming you want to extract the string inside (( and )):

VAR="abc((def))ghi"
echo "$VAR"
VAR=${VAR##*((}
VAR=${VAR%%))*}
echo "$VAR"

## cuts away the longest string from the beginning; # cuts away the shortest string from the beginning; %% cuts away the longest string at the end; % cuts away the shortes string at the end

于 2013-04-20T20:58:31.023 回答
0

文件 :

$ 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
于 2013-04-20T20:51:16.610 回答
0

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-20T21:29:25.237 回答