0

I need to mass replace a string in many sql files (preferably in shell). The string I need to replace looks like this (20120408, 20120409, 20120410) but it can have any amount of whitespace inside (not in the numbers though). For example it can be

(20120408,  20120409   , 20120410 ) or 
(    20120408   , 20120409   ,    20120410)

I need to put some other string, let's say (XXX) instead of it. The brackets must stay.

Thanks in advance for your help.

EDIT.

Let me be more specific. I need only strings of the types above converted to (xxx). This means that the numbers are important. I do NOT want just anything between two brackets to be replaced, so if I have (19002344) I don't want it replaced.

The only thing that needs to be replaced is the three numbers above in brackets with any amount of whitespace in between.

Hope this clear the misunderstanding.

3
  • can you paste expected output please? you see the current two answers doing different things. :) Commented Mar 15, 2013 at 15:06
  • @kent - just about to add the same question; realized I'm utterly clueless about the expected output :-) Commented Mar 15, 2013 at 15:10
  • @FredrikPihl I am guessing.. 2nd output in your answer would be the right one... but he doesn't want to hard code those CSVs. just guess... Commented Mar 15, 2013 at 15:13

1 Answer 1

1

Howabout this?

sed -e 's/20120408/XXX/' -e 's/20120409/YYY/' -e 's/20120410/ZZZ/' input

output:

(XXX,  YYY   , ZZZ ) or 
(    XXX   , YYY   ,    ZZZ)

or everything in one-go:

$ sed 's/20120408\|20120409\|20120410/XXX/g' input
(XXX,  XXX   , XXX ) or 
(    XXX   , XXX   ,    XXX)

update

$ cat input
(    20120408,  20120409   , 20120410 )
( 20120408,  20120409   , 19002344 )

$ sed 's/^(\s*20120408\s*,\s*20120409\s*,\s*20120410\s*)$/(xxx)/' input
(xxx)
( 20120408,  20120409   , 19002344 )

The above assumes one entry per line; if there are several use this instead:

sed 's/(\s*20120408\s*,\s*20120409\s*,\s*20120410\s*)/(xxx)/g' input
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, can't explain myself properly. I don't want each number replaced. The end result should be only (XXX). And I only want the replacement if those numbers are in brackets...
This looks good. Thanks, I'll try it and let you know. Shouldn't there be a /g switch to change all instances?
well, I'm assuming there's only one per line but if there are several then add the g-switch and remove the anchoring , i.e. the ^ and the $
Thanks, can be more then one on a line sometimes so I will use the second version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.