0

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 ?

Franck
  • 325
  • 3
  • 7
  • how to escape regular expressions in bash http://stackoverflow.com/questions/11856054/bash-easy-way-to-pass-a-raw-string-to-grep/16951928#16951928 – Riccardo Galli Jun 06 '13 at 00:29

1 Answers1

0

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.

David Foerster
  • 1,461
  • 1
  • 14
  • 23
Chris
  • 2,987
  • 2
  • 20
  • 21