how to add opening and closing brackets in the condition using unix shell script
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have conditions in a file, that has around 10 thousend lines. As shown below.
COLUMN_NAME NOT IN 1234534
COLUMN_NAME1 NOT IN 34252
COLUMN_NAME_2 not in 67496575
COLUMN_NAME NOT in 1234534
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN 1234534,453535
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN 1234534
COLUMN_NAME NOT IN 1234534
fdfsdf COLUMN_NAME not in 1234534
COLUMN_NAME not in 1234534
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
I want to use the sed command to add the brackets after the IN clause. I want to replace the conditions above result in output like below.
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (3422)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
shell unix sed
|
show 1 more comment
I have conditions in a file, that has around 10 thousend lines. As shown below.
COLUMN_NAME NOT IN 1234534
COLUMN_NAME1 NOT IN 34252
COLUMN_NAME_2 not in 67496575
COLUMN_NAME NOT in 1234534
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN 1234534,453535
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN 1234534
COLUMN_NAME NOT IN 1234534
fdfsdf COLUMN_NAME not in 1234534
COLUMN_NAME not in 1234534
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
I want to use the sed command to add the brackets after the IN clause. I want to replace the conditions above result in output like below.
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (3422)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
shell unix sed
sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename
– Samuel Kirschner
Nov 15 '18 at 16:51
@SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn
– ram
Nov 15 '18 at 17:11
@oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question
– ram
Nov 15 '18 at 17:17
1
@oguzismail I added the new edge cases please check
– ram
Nov 15 '18 at 17:26
Please clarify — are the[NOT] IN
conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)
– Stephen P
Nov 15 '18 at 17:34
|
show 1 more comment
I have conditions in a file, that has around 10 thousend lines. As shown below.
COLUMN_NAME NOT IN 1234534
COLUMN_NAME1 NOT IN 34252
COLUMN_NAME_2 not in 67496575
COLUMN_NAME NOT in 1234534
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN 1234534,453535
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN 1234534
COLUMN_NAME NOT IN 1234534
fdfsdf COLUMN_NAME not in 1234534
COLUMN_NAME not in 1234534
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
I want to use the sed command to add the brackets after the IN clause. I want to replace the conditions above result in output like below.
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (3422)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
shell unix sed
I have conditions in a file, that has around 10 thousend lines. As shown below.
COLUMN_NAME NOT IN 1234534
COLUMN_NAME1 NOT IN 34252
COLUMN_NAME_2 not in 67496575
COLUMN_NAME NOT in 1234534
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN 1234534,453535
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN 1234534
COLUMN_NAME NOT IN 1234534
fdfsdf COLUMN_NAME not in 1234534
COLUMN_NAME not in 1234534
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
I want to use the sed command to add the brackets after the IN clause. I want to replace the conditions above result in output like below.
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (3422)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
shell unix sed
shell unix sed
edited Nov 15 '18 at 18:39
oguz ismail
6,36541330
6,36541330
asked Nov 15 '18 at 16:34
ramram
4818
4818
sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename
– Samuel Kirschner
Nov 15 '18 at 16:51
@SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn
– ram
Nov 15 '18 at 17:11
@oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question
– ram
Nov 15 '18 at 17:17
1
@oguzismail I added the new edge cases please check
– ram
Nov 15 '18 at 17:26
Please clarify — are the[NOT] IN
conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)
– Stephen P
Nov 15 '18 at 17:34
|
show 1 more comment
sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename
– Samuel Kirschner
Nov 15 '18 at 16:51
@SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn
– ram
Nov 15 '18 at 17:11
@oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question
– ram
Nov 15 '18 at 17:17
1
@oguzismail I added the new edge cases please check
– ram
Nov 15 '18 at 17:26
Please clarify — are the[NOT] IN
conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)
– Stephen P
Nov 15 '18 at 17:34
sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename
– Samuel Kirschner
Nov 15 '18 at 16:51
sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename
– Samuel Kirschner
Nov 15 '18 at 16:51
@SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn
– ram
Nov 15 '18 at 17:11
@SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn
– ram
Nov 15 '18 at 17:11
@oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question
– ram
Nov 15 '18 at 17:17
@oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question
– ram
Nov 15 '18 at 17:17
1
1
@oguzismail I added the new edge cases please check
– ram
Nov 15 '18 at 17:26
@oguzismail I added the new edge cases please check
– ram
Nov 15 '18 at 17:26
Please clarify — are the
[NOT] IN
conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)– Stephen P
Nov 15 '18 at 17:34
Please clarify — are the
[NOT] IN
conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)– Stephen P
Nov 15 '18 at 17:34
|
show 1 more comment
2 Answers
2
active
oldest
votes
This would be what you are looking for
sed -i .bak 's/(sins+)([^() ]+)/1(2)/i' file
Explanation
s/regexp/replacement/flags
tries to match input line against regexp
, and if it manages, replaces the matched part with replacement
.
regexp
part:(sins+)
(ERE:(sins+)
) matches a space followed byin
plus one or more spaces, and keeps the matched portion in capture group 1,([^() ]+)
(ERE:([^() ]+)
) matches a group of non-space, non-parenthesis chars and keeps it in capture group 2,
replacement
part:1(2)
expands up to<capture group 1>(<capture group 2>)
,
flags
part:i
makes the matching case-insensitive.
1
Thanks it works Could you please explain the code or please provide some reference, So I can go through?
– ram
Nov 15 '18 at 17:37
1
Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help
– ram
Nov 15 '18 at 17:48
1
Thank you it helps
– ram
Nov 15 '18 at 17:56
add a comment |
Lets take it step by step.
I guess the best rule would be:
- IN (case insensetive)
IN
- followed by: a number
[0-9]+
- optionally followed by multiple (
*
): comma and number,[0-9]
This would result in the regex:IN[0-9]+(,[0-9]+)*
Next step is to add mandatory (s
) and optional (s*
) whitespace:
sINs*[0-9]+s*(,s*[0-9]+s*)*
Now to replace sINs*
[0-9]+s*(,s*[0-9]+s*)*
with 1(2)
it you need capture groups.
(sINs*)([0-9]+s*(,s*[0-9]+s*)*)
Now the regex has to go inside s//1(2)/i
(the i
means case insensitive)
s/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i
And at last we have a shell command, including a backup in case of mistakes.
sed -i.bak 's/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i' filename
I tested it on your example data and the output is:
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (34252)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323979%2fhow-to-add-opening-and-closing-brackets-in-the-condition-using-unix-shell-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This would be what you are looking for
sed -i .bak 's/(sins+)([^() ]+)/1(2)/i' file
Explanation
s/regexp/replacement/flags
tries to match input line against regexp
, and if it manages, replaces the matched part with replacement
.
regexp
part:(sins+)
(ERE:(sins+)
) matches a space followed byin
plus one or more spaces, and keeps the matched portion in capture group 1,([^() ]+)
(ERE:([^() ]+)
) matches a group of non-space, non-parenthesis chars and keeps it in capture group 2,
replacement
part:1(2)
expands up to<capture group 1>(<capture group 2>)
,
flags
part:i
makes the matching case-insensitive.
1
Thanks it works Could you please explain the code or please provide some reference, So I can go through?
– ram
Nov 15 '18 at 17:37
1
Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help
– ram
Nov 15 '18 at 17:48
1
Thank you it helps
– ram
Nov 15 '18 at 17:56
add a comment |
This would be what you are looking for
sed -i .bak 's/(sins+)([^() ]+)/1(2)/i' file
Explanation
s/regexp/replacement/flags
tries to match input line against regexp
, and if it manages, replaces the matched part with replacement
.
regexp
part:(sins+)
(ERE:(sins+)
) matches a space followed byin
plus one or more spaces, and keeps the matched portion in capture group 1,([^() ]+)
(ERE:([^() ]+)
) matches a group of non-space, non-parenthesis chars and keeps it in capture group 2,
replacement
part:1(2)
expands up to<capture group 1>(<capture group 2>)
,
flags
part:i
makes the matching case-insensitive.
1
Thanks it works Could you please explain the code or please provide some reference, So I can go through?
– ram
Nov 15 '18 at 17:37
1
Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help
– ram
Nov 15 '18 at 17:48
1
Thank you it helps
– ram
Nov 15 '18 at 17:56
add a comment |
This would be what you are looking for
sed -i .bak 's/(sins+)([^() ]+)/1(2)/i' file
Explanation
s/regexp/replacement/flags
tries to match input line against regexp
, and if it manages, replaces the matched part with replacement
.
regexp
part:(sins+)
(ERE:(sins+)
) matches a space followed byin
plus one or more spaces, and keeps the matched portion in capture group 1,([^() ]+)
(ERE:([^() ]+)
) matches a group of non-space, non-parenthesis chars and keeps it in capture group 2,
replacement
part:1(2)
expands up to<capture group 1>(<capture group 2>)
,
flags
part:i
makes the matching case-insensitive.
This would be what you are looking for
sed -i .bak 's/(sins+)([^() ]+)/1(2)/i' file
Explanation
s/regexp/replacement/flags
tries to match input line against regexp
, and if it manages, replaces the matched part with replacement
.
regexp
part:(sins+)
(ERE:(sins+)
) matches a space followed byin
plus one or more spaces, and keeps the matched portion in capture group 1,([^() ]+)
(ERE:([^() ]+)
) matches a group of non-space, non-parenthesis chars and keeps it in capture group 2,
replacement
part:1(2)
expands up to<capture group 1>(<capture group 2>)
,
flags
part:i
makes the matching case-insensitive.
edited Nov 19 '18 at 6:09
answered Nov 15 '18 at 17:18
oguz ismailoguz ismail
6,36541330
6,36541330
1
Thanks it works Could you please explain the code or please provide some reference, So I can go through?
– ram
Nov 15 '18 at 17:37
1
Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help
– ram
Nov 15 '18 at 17:48
1
Thank you it helps
– ram
Nov 15 '18 at 17:56
add a comment |
1
Thanks it works Could you please explain the code or please provide some reference, So I can go through?
– ram
Nov 15 '18 at 17:37
1
Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help
– ram
Nov 15 '18 at 17:48
1
Thank you it helps
– ram
Nov 15 '18 at 17:56
1
1
Thanks it works Could you please explain the code or please provide some reference, So I can go through?
– ram
Nov 15 '18 at 17:37
Thanks it works Could you please explain the code or please provide some reference, So I can go through?
– ram
Nov 15 '18 at 17:37
1
1
Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help
– ram
Nov 15 '18 at 17:48
Apologies I am very new in unix could you please explain /1(2)/. Thank you for your Help
– ram
Nov 15 '18 at 17:48
1
1
Thank you it helps
– ram
Nov 15 '18 at 17:56
Thank you it helps
– ram
Nov 15 '18 at 17:56
add a comment |
Lets take it step by step.
I guess the best rule would be:
- IN (case insensetive)
IN
- followed by: a number
[0-9]+
- optionally followed by multiple (
*
): comma and number,[0-9]
This would result in the regex:IN[0-9]+(,[0-9]+)*
Next step is to add mandatory (s
) and optional (s*
) whitespace:
sINs*[0-9]+s*(,s*[0-9]+s*)*
Now to replace sINs*
[0-9]+s*(,s*[0-9]+s*)*
with 1(2)
it you need capture groups.
(sINs*)([0-9]+s*(,s*[0-9]+s*)*)
Now the regex has to go inside s//1(2)/i
(the i
means case insensitive)
s/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i
And at last we have a shell command, including a backup in case of mistakes.
sed -i.bak 's/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i' filename
I tested it on your example data and the output is:
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (34252)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
add a comment |
Lets take it step by step.
I guess the best rule would be:
- IN (case insensetive)
IN
- followed by: a number
[0-9]+
- optionally followed by multiple (
*
): comma and number,[0-9]
This would result in the regex:IN[0-9]+(,[0-9]+)*
Next step is to add mandatory (s
) and optional (s*
) whitespace:
sINs*[0-9]+s*(,s*[0-9]+s*)*
Now to replace sINs*
[0-9]+s*(,s*[0-9]+s*)*
with 1(2)
it you need capture groups.
(sINs*)([0-9]+s*(,s*[0-9]+s*)*)
Now the regex has to go inside s//1(2)/i
(the i
means case insensitive)
s/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i
And at last we have a shell command, including a backup in case of mistakes.
sed -i.bak 's/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i' filename
I tested it on your example data and the output is:
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (34252)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
add a comment |
Lets take it step by step.
I guess the best rule would be:
- IN (case insensetive)
IN
- followed by: a number
[0-9]+
- optionally followed by multiple (
*
): comma and number,[0-9]
This would result in the regex:IN[0-9]+(,[0-9]+)*
Next step is to add mandatory (s
) and optional (s*
) whitespace:
sINs*[0-9]+s*(,s*[0-9]+s*)*
Now to replace sINs*
[0-9]+s*(,s*[0-9]+s*)*
with 1(2)
it you need capture groups.
(sINs*)([0-9]+s*(,s*[0-9]+s*)*)
Now the regex has to go inside s//1(2)/i
(the i
means case insensitive)
s/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i
And at last we have a shell command, including a backup in case of mistakes.
sed -i.bak 's/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i' filename
I tested it on your example data and the output is:
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (34252)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
Lets take it step by step.
I guess the best rule would be:
- IN (case insensetive)
IN
- followed by: a number
[0-9]+
- optionally followed by multiple (
*
): comma and number,[0-9]
This would result in the regex:IN[0-9]+(,[0-9]+)*
Next step is to add mandatory (s
) and optional (s*
) whitespace:
sINs*[0-9]+s*(,s*[0-9]+s*)*
Now to replace sINs*
[0-9]+s*(,s*[0-9]+s*)*
with 1(2)
it you need capture groups.
(sINs*)([0-9]+s*(,s*[0-9]+s*)*)
Now the regex has to go inside s//1(2)/i
(the i
means case insensitive)
s/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i
And at last we have a shell command, including a backup in case of mistakes.
sed -i.bak 's/(sINs*)([0-9]+s*(,s*[0-9]+s*)*)/1(2)/i' filename
I tested it on your example data and the output is:
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME1 NOT IN (34252)
COLUMN_NAME_2 not in (67496575)
COLUMN_NAME NOT in (1234534)
foo COLUMN_NAME NOT IN (1234534,453535) rest of the line
COLUMN_NAME NOT IN (1234534,453535)
columnsd not in (23123124232,6464777) rest on the line
COLUMN_NAME NOT IN (1234534)
COLUMN_NAME NOT IN (1234534)
fdfsdf COLUMN_NAME not in (1234534)
COLUMN_NAME not in (1234534)
column NOT IN (6764577,434545)
COLUMN_NAME not in (1234534)
answered Nov 15 '18 at 17:51
Samuel KirschnerSamuel Kirschner
9351717
9351717
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323979%2fhow-to-add-opening-and-closing-brackets-in-the-condition-using-unix-shell-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
sed -i.bak 's/(sins*)([0-9]+)/1(2)/' filename
– Samuel Kirschner
Nov 15 '18 at 16:51
@SamuelKirschner sir its not working for case COLUMN_NAME NOT IN 1234534 and also could you please give me some link reference so I can learn
– ram
Nov 15 '18 at 17:11
@oguzismail awk also adding () where it is already there like COLUMN_NAME NOT IN ((1234534,453535)) and also after the condition also we have some constant Apologies mixed in question
– ram
Nov 15 '18 at 17:17
1
@oguzismail I added the new edge cases please check
– ram
Nov 15 '18 at 17:26
Please clarify — are the
[NOT] IN
conditions always numeric? You don't explicitly state that, and you really need to be specific when defining this sort of problem. I think Samuel Kirschner's comments are on-track (but I haven't tested)– Stephen P
Nov 15 '18 at 17:34