|
|
Grep Replace??
|
|
|
|
Mac Elite
Join Date: Jan 2001
Status:
Offline
|
|
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!
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2001
Status:
Offline
|
|
Thanks - I figured out how to do it in BBedit using grep and for replace, i Used "&"
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Nov 2001
Location: Oregon
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Feb 2001
Location: Manhattan
Status:
Offline
|
|
or perl:
Code:
% perl -pi -e 's/(VALUES \(\d+,)/$1'/g' FILENAME
...which is pretty handy too.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|