转义包含双引号、括号的正则表达式的 bash 变量
这对于 regex pro 来说一定是显而易见的,但我找不到任何方法来实现这一点。
我想在对与正则表达式匹配的行进行分组后将一个大的 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 它,
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
这导致错误 ( 或 (
有人得到提示或替代解决方案来实现这一点吗?
This must be obvious for regex pro, but I can't find any way to accomplish that.
I want to split a large sql file into several ones after grouping lines that match a regular expression...
So I got an array containing country codes.
I got a 700mb file with thousand of rows starting with the country code.
I want to split the file into several files depending on this country code.
For example a line is :
("ZA", "EN", 11564, "ZA-WC", "Western Cape", "West Coast (DC1)", "Swartland", "", "7310", "Moorreesburg", "", "", -33.15528, 18.65639, "Africa/Johannesburg", "UTC+2", "N"),
So I want to match ("ZA",
I know how to grep it through bash,
grep '("ZA"' data.sql
My problem is when i want to iterate with a bash script and encapsulate result into a var to redirect the output to a file... I stumble upon the double quote, parentheses escaping...
#!/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
This result in error with ( or (
Anyone got hints or alternative solution to accomplish that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两个错误:
不计算单引号内的 Shell 变量。
与
grep
一起使用<前><代码>"^(\"${国家/地区代码}"
匹配字符串。请勿转义
(
-字符。There are two errors:
Shell-Variables inside single quotes are not evaluated.
With
grep
useto match the string. Do not escape the
(
-character.