Escaping bash variables for regular expression containing double quote, parentheses

£可爱£侵袭症+ 提交于 2019-12-24 19:27:36

问题


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 ?


回答1:


There are two errors:

  1. Shell-Variables inside single quotes are not evaluated.

  2. With grep use

    "^(\"${country_code}" 
    

    to match the string. Do not escape the (-character.



来源:https://stackoverflow.com/questions/7810591/escaping-bash-variables-for-regular-expression-containing-double-quote-parenthe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!