Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 转义包含双引号、圆括号的正则表达式的bash变量_Regex_Bash_Escaping_Split - Fatal编程技术网

Regex 转义包含双引号、圆括号的正则表达式的bash变量

Regex 转义包含双引号、圆括号的正则表达式的bash变量,regex,bash,escaping,split,Regex,Bash,Escaping,Split,这对于regex pro来说是显而易见的,但我找不到任何方法来实现这一点 我想在对与正则表达式匹配的行进行分组后,将一个大型sql文件拆分为多个sql文件 所以我得到了一个包含国家代码的数组。 我得到了一个700mb的文件,其中有数千行以国家代码开头。 我想根据此国家/地区代码将文件拆分为多个文件 例如,一行是: ("ZA", "EN", 11564, "ZA-WC", "Western Cape", "West Coast (DC1)", "Swartland", "", "7310", "M

这对于regex pro来说是显而易见的,但我找不到任何方法来实现这一点

我想在对与正则表达式匹配的行进行分组后,将一个大型sql文件拆分为多个sql文件

所以我得到了一个包含国家代码的数组。 我得到了一个700mb的文件,其中有数千行以国家代码开头。 我想根据此国家/地区代码将文件拆分为多个文件

例如,一行是:

("ZA", "EN", 11564, "ZA-WC", "Western Cape", "West Coast (DC1)", "Swartland", "", "7310", "Moorreesburg", "", "", -33.15528, 18.65639, "Africa/Johannesburg", "UTC+2", "N"),
所以我想匹配
(“ZA”,
我知道如何在bash中表现出色

grep '("ZA"' data.sql
我的问题是,当我想用bash脚本进行迭代并将结果封装到一个var中以将输出重定向到一个文件时……我偶然发现了双引号、括号转义

#!/bin/bash

country_code_list_path="country_code.txt"
for country_code in $(cat "$country_code_list_path");
do echo $country_code;
result=`grep '^\(\""$country_code"\", ' geodata.sql| tail -1`;
echo $result;
done
这将导致(或(

有人得到了实现这一点的提示或替代解决方案吗?

有两个错误:

  • 不计算单引号内的Shell变量

  • 使用
    grep
    使用

    "^(\"${country_code}" 
    
    匹配字符串。不要转义
    )字符


  • 如何在bash中转义正则表达式