有很多类似的问题,但我找不到解决问题的方法。我可能不知道该怎么去谷歌。
我正在尝试创建一对数组,然后通过索引循环遍历。数组的内容由以下命令生成:
SetEnvIF X-Forwarded-For "192.168.135.159" AllowIP
SetEnvIF X-Forwarded-For "192.168.135.135" AllowIP
AuthType Basic
AuthName "admin"
AuthUserFile "/var/www/domain.com/cms/.htpasswd"
<RequireAll>
Require env AllowIP
require valid-user
</RequireAll>
但是制作一个像这样的数组
$ cat small | grep -Po "(?<=Query: ).+$" | grep -v "use"
select count(*) from batting_small
select count(*) from batting_small_parquet
select count(*) from batting_small_parquet_stats
select yearid, count(*) from batting_small_parquet group by yearId
select yearid, count(*) from batting_small_parquet_stats group by yearId
select min(hr), max(hr) from batting_small
select min(hr), max(hr) from batting_small_parquet
select min(hr), max(hr) from batting_small_parquet_stats
...
按字而不是按行分割。我该怎么分开?
答案 0 :(得分:1)
使用mapfile
bash builtin,并从进程替换中重定向grep命令:
mapfile -t queries < <( grep -Po "(?<=Query: ).+$" small | grep -v "use")
答案 1 :(得分:1)
您可以在-z
中使用grep
选项生成NUL终止的输出,然后在while IFS= read -d ''
循环中使用进程替换:
while IFS= read -d '' -r line; do
echo "<<$line>>"
done < <(ggrep -zPo '(?m)(?!.*use)(?<=Query: ).+$' small)
<<select count(*) from batting_small>>
<<select count(*) from batting_small_parquet>>
<<select count(*) from batting_small_parquet_stats>>
<<select yearid, count(*) from batting_small_parquet group by yearId>>
<<select yearid, count(*) from batting_small_parquet_stats group by yearId>>
<<select min(hr), max(hr) from batting_small>>
<<select min(hr), max(hr) from batting_small_parquet>>
<<select min(hr), max(hr) from batting_small_parquet_stats>>
将echo "<<$line>>"
行更改为您的实际代码。
另请注意,使用(?m)
在正则表达式中启用MULTILINE
模式,使用否定前瞻(?!.*use)
以避免其他grep -v
。