定期EX pression逃脱bash的变量包含双引号,括号 [英] Escaping bash variables for regular expression containing double quote, parentheses
问题描述
这必须是正则表达式亲明显,但我无法找到任何方式来实现这一目标。
This must be obvious for regex pro, but I can't find any way to accomplish that.
我想分组匹配正前pression该行之后拆分大型SQL文件分成几个的...
I want to split a large sql file into several ones after grouping lines that match a regular expression...
所以我就含有国家codeS的数组。
我有一个700MB的文件,开始在全国code行万。
我想将文件分割成取决于这个国家code的几个文件。
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.
例如一个行是:
("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显示它,
So I want to match ("ZA",
I know how to grep it through bash,
grep '("ZA"' data.sql
我的问题是,当我想用bash脚本迭代和结果封装成一个变种将输出重定向到一个文件...我偶然发现了双引号,圆括号逃避...
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
这导致错误与(或(
任何人有提示或替代解决方案来实现这一目标?
Anyone got hints or alternative solution to accomplish that ?
推荐答案
有两个误区:
-
单引号内壳中的变量不进行评估。
Shell-Variables inside single quotes are not evaluated.
通过的grep
使用
"^(\"${country_code}"
要匹配字符串。不要逃避(
字符数限制。
to match the string. Do not escape the (
-character.
这篇关于定期EX pression逃脱bash的变量包含双引号,括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!