Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Grep Replace??

Grep Replace??
Thread Tools
timmerk
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Jun 30, 2004, 02:30 PM
 
Hi,

I was wondering if anyone knew how to do this using grep and other unix tools -

I am editing a database insert file and need to replace stuff like:

VALUES (1,2004-01-5',
VALUES (124,2014-01-5',
VALUES (14,2016-3-15',

to

VALUES (1,'2004-01-5',
VALUES (124,'2014-01-5',
VALUES (14,'2016-3-15',

so basically, I just need to insert an ' before the dates. The patten to find this is:

VALUES \([0-9]+,

but how can I replace all found values with the same thing, but with ' at the end?

Thanks!
     
VEGAN
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Jul 1, 2004, 02:55 AM
 
Originally posted by timmerk:
Hi,

I was wondering if anyone knew how to do this using grep and other unix tools -

I am editing a database insert file and need to replace stuff like:

VALUES (1,2004-01-5',
VALUES (124,2014-01-5',
VALUES (14,2016-3-15',

to

VALUES (1,'2004-01-5',
VALUES (124,'2014-01-5',
VALUES (14,'2016-3-15',

so basically, I just need to insert an ' before the dates. The patten to find this is:

VALUES \([0-9]+,

but how can I replace all found values with the same thing, but with ' at the end?

Thanks!
Use, for example, `sed'

Let's assume you have the DATA in file called DATA.txt then you can do something like this:

sed "s/,/,'/" DATA.txt

This will only replace the first `,'-sign (with `,'' as you wanted).
If your problem is more complex, post it here. But the description you gave makes it seem pretty easy to solve
     
timmerk  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Jul 1, 2004, 09:34 AM
 
Thanks - I figured out how to do it in BBedit using grep and for replace, i Used "&"
     
Rainy Day
Grizzled Veteran
Join Date: Nov 2001
Location: Oregon
Status: Offline
Reply With Quote
Jul 11, 2004, 01:11 PM
 
The command line tool grep does not do editing nor replacing. But it did pioneer the use of regular expressions. Sometimes people refer to "grep" when what they really mean are "grep-like regular expressions," or more accurately, "regular expressions."

Two popular command line tools for editing using regular expressions are sed and perl.
     
VEGAN
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Jul 12, 2004, 02:32 AM
 
Originally posted by Rainy Day:
The command line tool grep does not do editing nor replacing. But it did pioneer the use of regular expressions. Sometimes people refer to "grep" when what they really mean are "grep-like regular expressions," or more accurately, "regular expressions."

Two popular command line tools for editing using regular expressions are sed and perl.
Besides `sed' I rather like `awk' too
     
ameat
Dedicated MacNNer
Join Date: Feb 2001
Location: Manhattan
Status: Offline
Reply With Quote
Jul 12, 2004, 05:55 PM
 
or perl:
Code:
% perl -pi -e 's/(VALUES \(\d+,)/$1'/g' FILENAME
...which is pretty handy too.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:05 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,