将字符串直接拆分为数组

时间:2021-07-27 21:30:54

Suppose I want to pass a string to awk so that once I split it (on a pattern) the substrings become the indexes (not the values) of an associative array.

假设我想将一个字符串传递给awk,这样一旦我将其分割(在模式上),子字符串就会成为关联数组的索引(而不是值)。

Like so:

$ awk -v s="A:B:F:G" 'BEGIN{ # easy, but can these steps be combined?
                            split(s,temp,":")  # temp[1]="A",temp[2]="B"...
                            for (e in temp) arr[temp[e]] #arr["A"], arr["B"]...
                            for (e in arr) print e 
                            }'
A
B
F
G

Is there a awkism or gawkism that would allow the string s to be directly split into its components with those components becoming the index entries in arr?

是否有一个awkism或gawkism允许字符串s直接拆分为其组件,这些组件成为arr中的索引条目?


The reason is (bigger picture) is I want something like this (pseudo awk):

原因是(更大的图片)我想要这样的东西(伪awk):

awk -v s="1,4,55" 'BEGIN{[arr to arr["1"],arr["5"],arr["55"]} $3 in arr {action}'

3 个解决方案

#1


3  

No, there is no better way to map separated substrings to array indices than:

不,没有更好的方法将分离的子串映射到数组索引,而不是:

split(str,tmp); for (i in tmp) arr[tmp[i]]

FWIW if you don't like that approach for doing what your final pseudo-code does:

FWIW如果您不喜欢这种方法来执行最终的伪代码:

awk -v s="1,4,55" 'BEGIN{split(s,tmp,/,/); for (i in tmp) arr[tmp[i]]} $3 in arr{action}'

then another way to get the same behavior is

然后另一种获得相同行为的方法是

awk -v s=",1,4,55," 'index(s,","$3","){action}'

#2


1  

Probably useless and unnecessarily complex but I'll open the game with while, match and substr:

可能无用且不必要的复杂但我会用while,match和substr打开游戏:

$ awk -v s="A:B:F:G" '
BEGIN {
    while(match(s,/[^:]+/)) {
        a[substr(s,RSTART,RLENGTH)]
        s=substr(s,RSTART+RLENGTH)
    }
    for(i in a)
        print i
}'
A
B
F
G

I'm eager to see (if there are) some useful solutions. I tried playing around with asorts and such.

我很想看到(如果有的话)一些有用的解决方案。我尝试过使用asorts等。

#3


1  

Other way kind awkism

其他方式类似的awkism

cat file

1 hi
2 hello
3 bonjour
4 hola
5 konichiwa

Run it,

awk 'NR==FNR{d[$1]; next}$1 in d' RS="," <(echo "1,2,4") RS="\n" file

you get,

1 hi
2 hello
4 hola

#1


3  

No, there is no better way to map separated substrings to array indices than:

不,没有更好的方法将分离的子串映射到数组索引,而不是:

split(str,tmp); for (i in tmp) arr[tmp[i]]

FWIW if you don't like that approach for doing what your final pseudo-code does:

FWIW如果您不喜欢这种方法来执行最终的伪代码:

awk -v s="1,4,55" 'BEGIN{split(s,tmp,/,/); for (i in tmp) arr[tmp[i]]} $3 in arr{action}'

then another way to get the same behavior is

然后另一种获得相同行为的方法是

awk -v s=",1,4,55," 'index(s,","$3","){action}'

#2


1  

Probably useless and unnecessarily complex but I'll open the game with while, match and substr:

可能无用且不必要的复杂但我会用while,match和substr打开游戏:

$ awk -v s="A:B:F:G" '
BEGIN {
    while(match(s,/[^:]+/)) {
        a[substr(s,RSTART,RLENGTH)]
        s=substr(s,RSTART+RLENGTH)
    }
    for(i in a)
        print i
}'
A
B
F
G

I'm eager to see (if there are) some useful solutions. I tried playing around with asorts and such.

我很想看到(如果有的话)一些有用的解决方案。我尝试过使用asorts等。

#3


1  

Other way kind awkism

其他方式类似的awkism

cat file

1 hi
2 hello
3 bonjour
4 hola
5 konichiwa

Run it,

awk 'NR==FNR{d[$1]; next}$1 in d' RS="," <(echo "1,2,4") RS="\n" file

you get,

1 hi
2 hello
4 hola