Escaping bash variables for regular expression con

2019-08-31 08:22发布

问题:

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.