如何查找和提取括号之间出现的所有单词?

时间:2015-06-29 19:10:10

标签: arrays bash awk sed grep

如何将所有出现在文本文件中括号之间的单词放入数组中并将其替换为该数组中的随机单词?

cat math.txt
First: {736|172|201|109} {+|-|*|%|/} {21|62|9|1|0}
Second: John had {22|12|15} apples and lost {2|4|3}

我需要输出:

First: 172-9
Second: John had 15 apples and lost 4

3 个答案:

答案 0 :(得分:2)

这在awk中是微不足道的:

$ cat tst.awk
BEGIN{ srand() }
{
    for (i=1; i<=NF; i++) {
        if ( match($i,/{[^}]+}/) ) {
            n = split(substr($i,RSTART+1,RLENGTH-2),arr,/\|/)
            idx = int(rand() * (n-1)) + 1
            $i  = arr[idx]
        }
        printf "%s%s", $i, (i<NF?OFS:ORS)
   }
}

$ awk -f tst.awk file
First: 172 - 9
Second: John had 22 apples and lost 2
$ awk -f tst.awk file
First: 201 - 9
Second: John had 12 apples and lost 2
$ awk -f tst.awk file
First: 201 + 62
Second: John had 12 apples and lost 2
$ awk -f tst.awk file
First: 201 + 1
Second: John had 12 apples and lost 4

只需检查设置idx的行上的数学运算 - 我认为这是正确的,但没有考虑到它。

答案 1 :(得分:1)

BEGIN{srand()}

如何运作

  • for (i=1;i<=NF;i++) {if (substr($i,1,1)=="{") {split(substr($i,2,length($i)-2),a,"|"); j=1+int(rand()*length(a)); $i=a[j]}

    这会初始化随机数生成器。

  • {

    这会遍历每个字段。如果任何字段以substr开头,则|用于删除字段的第一个和最后一个字符,剩余部分将a拆分为数组j的分隔符。然后,选择随机索引a到数组a[j]。最后,该字段将替换为print

  • awk 'BEGIN{srand()} { for (i=1;i<=NF;i++) { if (substr($i,1,1)=="{") { split(substr($i,2,length($i)-2),a,"|") j=1+int(rand()*length(a)) $i=a[j] } } print }' math.txt

    打印上面修改过的行。

与上面相同的代码,但重新格式化为多行,是:

match.txt

空间修正问题

假设$ cat math.txt First: {736|172|201|109} {+|-|*|%|/} {21|62|9|1|0} Second: John had {22|12|15} apples and lost {2|4|3} Third: John had {22 22|12 12|15 15} apples and lost {2 2|4 4|3 3} 现在看起来像:

{...}

最后一行在$ awk -F'[{}]' 'BEGIN{srand()} {for (i=2;i<=NF;i+=2) {n=split($i,a,"|"); j=1+int(n*rand()); $i=a[j]}; print}' math.txt First: 736 + 62 Second: John had 12 apples and lost 3 Third: John had 15 15 apples and lost 2 2 内有空格。这改变了awk如何划分字段。对于这种情况,我们可以使用:

-F'[{}]'

工作原理:

  • }

    这告诉awk使用{BEGIN{srand()}作为字段分隔符。

  • {for (i=2;i<=NF;i+=2) {n=split($i,a,"|"); j=1+int(n*rand()); $i=a[j]}

    初始化随机数生成器

  • |

    使用我们对字段分隔符的新定义,偶数字段是大括号内的字段。因此,我们将这些字段拆分为$i=a[j]并随机选择一个字段并将字段分配给该字段:print

  • awk -F'[{}]' 'function rand2(n) { srand(); return 1 + int(rand() * n); } { for (i=1; i<=NF; i++) if (i%2) printf $i OFS; else { split($i, arr, "|"); printf arr[rand2(length(arr))] }; printf ORS }' math.txt First: 736 - 62 Second: John had 22 apples and lost 2

    如上所述修改了这一行,我们现在打印出来。

答案 2 :(得分:1)

你可以试试这个awk:

First:  172  *  9
Second: John had  12 apples and lost  4

First:  109  /  0
Second: John had  15 apples and lost  3

First:  201  %  1
Second: John had  12 apples and lost  3

...
...

上述更多行可能会产生:

var $hidden = $('#hidden');
$hidden.attr('tabindex', '-' + $hidden.attr('tabindex'));