Using sed how to replace comma before a specific string
I have a file temp.txt. Here wanted to replace comma(,) with || between Select and From keyword.
select emp_name,
emp_id,
loc
from emp_join ,
emp_loc
where emp_join.id = emp_loc.id
and join_date > to_date('2015-01-01','YYYY-MM-DD')
UNION
select emp_name,
emp_id,
loc
from emp_term,
emp_loc
where emp_term.id = emp_loc.id
and term_date = to_date('2015-01-01','YYYY-MM-DD');
I am using - sed command - sed 's/,/||/g' temp.txt
- but its replacing all comma in the file.
Is there a simple unix command that will allow me to do this?
How to do it using sed.
shell-script sed
migrated from serverfault.com Mar 29 '17 at 14:42
This question came from our site for system and network administrators.
add a comment |
I have a file temp.txt. Here wanted to replace comma(,) with || between Select and From keyword.
select emp_name,
emp_id,
loc
from emp_join ,
emp_loc
where emp_join.id = emp_loc.id
and join_date > to_date('2015-01-01','YYYY-MM-DD')
UNION
select emp_name,
emp_id,
loc
from emp_term,
emp_loc
where emp_term.id = emp_loc.id
and term_date = to_date('2015-01-01','YYYY-MM-DD');
I am using - sed command - sed 's/,/||/g' temp.txt
- but its replacing all comma in the file.
Is there a simple unix command that will allow me to do this?
How to do it using sed.
shell-script sed
migrated from serverfault.com Mar 29 '17 at 14:42
This question came from our site for system and network administrators.
@Rakesh Sharma command edited sed -e '/^select/,/loc/s/,/||/'
– c4f4t0r
Mar 30 '17 at 9:34
add a comment |
I have a file temp.txt. Here wanted to replace comma(,) with || between Select and From keyword.
select emp_name,
emp_id,
loc
from emp_join ,
emp_loc
where emp_join.id = emp_loc.id
and join_date > to_date('2015-01-01','YYYY-MM-DD')
UNION
select emp_name,
emp_id,
loc
from emp_term,
emp_loc
where emp_term.id = emp_loc.id
and term_date = to_date('2015-01-01','YYYY-MM-DD');
I am using - sed command - sed 's/,/||/g' temp.txt
- but its replacing all comma in the file.
Is there a simple unix command that will allow me to do this?
How to do it using sed.
shell-script sed
I have a file temp.txt. Here wanted to replace comma(,) with || between Select and From keyword.
select emp_name,
emp_id,
loc
from emp_join ,
emp_loc
where emp_join.id = emp_loc.id
and join_date > to_date('2015-01-01','YYYY-MM-DD')
UNION
select emp_name,
emp_id,
loc
from emp_term,
emp_loc
where emp_term.id = emp_loc.id
and term_date = to_date('2015-01-01','YYYY-MM-DD');
I am using - sed command - sed 's/,/||/g' temp.txt
- but its replacing all comma in the file.
Is there a simple unix command that will allow me to do this?
How to do it using sed.
shell-script sed
shell-script sed
asked Mar 29 '17 at 14:31
Ashish MriAshish Mri
11
11
migrated from serverfault.com Mar 29 '17 at 14:42
This question came from our site for system and network administrators.
migrated from serverfault.com Mar 29 '17 at 14:42
This question came from our site for system and network administrators.
@Rakesh Sharma command edited sed -e '/^select/,/loc/s/,/||/'
– c4f4t0r
Mar 30 '17 at 9:34
add a comment |
@Rakesh Sharma command edited sed -e '/^select/,/loc/s/,/||/'
– c4f4t0r
Mar 30 '17 at 9:34
@Rakesh Sharma command edited sed -e '/^select/,/loc/s/,/||/'
– c4f4t0r
Mar 30 '17 at 9:34
@Rakesh Sharma command edited sed -e '/^select/,/loc/s/,/||/'
– c4f4t0r
Mar 30 '17 at 9:34
add a comment |
2 Answers
2
active
oldest
votes
root@admin:~# cat N | sed 's/(.+),(.+)/1||2/g'
select emp_name,
emp_id,
loc
from emp_join ,
emp_loc
where emp_join.id = emp_loc.id
and join_date > to_date('2015-01-01'||'YYYY-MM-DD')
UNION
select emp_name,
emp_id,
loc
from emp_term,
emp_loc
where emp_term.id = emp_loc.id
and term_date = to_date('2015-01-01'||'YYYY-MM-DD');
When I am using the above command - cat temp.txt | sed 's/(.+),(.+)/1||2/g' - its replacing comma present in To_date function. join_date > to_date('2015-01-01'||'YYYY-MM-DD')
– Ashish Mri
Mar 30 '17 at 2:42
add a comment |
sed -e '/^select/,/^from/s/,/||/' temp.txt
Rakesh, when I am executing the command given by you, its replacing all commas present in the file. Wanted to replace the comma between Select and From keyword only.
– Ashish Mri
Mar 30 '17 at 2:39
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f354600%2fusing-sed-how-to-replace-comma-before-a-specific-string%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
root@admin:~# cat N | sed 's/(.+),(.+)/1||2/g'
select emp_name,
emp_id,
loc
from emp_join ,
emp_loc
where emp_join.id = emp_loc.id
and join_date > to_date('2015-01-01'||'YYYY-MM-DD')
UNION
select emp_name,
emp_id,
loc
from emp_term,
emp_loc
where emp_term.id = emp_loc.id
and term_date = to_date('2015-01-01'||'YYYY-MM-DD');
When I am using the above command - cat temp.txt | sed 's/(.+),(.+)/1||2/g' - its replacing comma present in To_date function. join_date > to_date('2015-01-01'||'YYYY-MM-DD')
– Ashish Mri
Mar 30 '17 at 2:42
add a comment |
root@admin:~# cat N | sed 's/(.+),(.+)/1||2/g'
select emp_name,
emp_id,
loc
from emp_join ,
emp_loc
where emp_join.id = emp_loc.id
and join_date > to_date('2015-01-01'||'YYYY-MM-DD')
UNION
select emp_name,
emp_id,
loc
from emp_term,
emp_loc
where emp_term.id = emp_loc.id
and term_date = to_date('2015-01-01'||'YYYY-MM-DD');
When I am using the above command - cat temp.txt | sed 's/(.+),(.+)/1||2/g' - its replacing comma present in To_date function. join_date > to_date('2015-01-01'||'YYYY-MM-DD')
– Ashish Mri
Mar 30 '17 at 2:42
add a comment |
root@admin:~# cat N | sed 's/(.+),(.+)/1||2/g'
select emp_name,
emp_id,
loc
from emp_join ,
emp_loc
where emp_join.id = emp_loc.id
and join_date > to_date('2015-01-01'||'YYYY-MM-DD')
UNION
select emp_name,
emp_id,
loc
from emp_term,
emp_loc
where emp_term.id = emp_loc.id
and term_date = to_date('2015-01-01'||'YYYY-MM-DD');
root@admin:~# cat N | sed 's/(.+),(.+)/1||2/g'
select emp_name,
emp_id,
loc
from emp_join ,
emp_loc
where emp_join.id = emp_loc.id
and join_date > to_date('2015-01-01'||'YYYY-MM-DD')
UNION
select emp_name,
emp_id,
loc
from emp_term,
emp_loc
where emp_term.id = emp_loc.id
and term_date = to_date('2015-01-01'||'YYYY-MM-DD');
answered Mar 29 '17 at 14:55
c4f4t0rc4f4t0r
50627
50627
When I am using the above command - cat temp.txt | sed 's/(.+),(.+)/1||2/g' - its replacing comma present in To_date function. join_date > to_date('2015-01-01'||'YYYY-MM-DD')
– Ashish Mri
Mar 30 '17 at 2:42
add a comment |
When I am using the above command - cat temp.txt | sed 's/(.+),(.+)/1||2/g' - its replacing comma present in To_date function. join_date > to_date('2015-01-01'||'YYYY-MM-DD')
– Ashish Mri
Mar 30 '17 at 2:42
When I am using the above command - cat temp.txt | sed 's/(.+),(.+)/1||2/g' - its replacing comma present in To_date function. join_date > to_date('2015-01-01'||'YYYY-MM-DD')
– Ashish Mri
Mar 30 '17 at 2:42
When I am using the above command - cat temp.txt | sed 's/(.+),(.+)/1||2/g' - its replacing comma present in To_date function. join_date > to_date('2015-01-01'||'YYYY-MM-DD')
– Ashish Mri
Mar 30 '17 at 2:42
add a comment |
sed -e '/^select/,/^from/s/,/||/' temp.txt
Rakesh, when I am executing the command given by you, its replacing all commas present in the file. Wanted to replace the comma between Select and From keyword only.
– Ashish Mri
Mar 30 '17 at 2:39
add a comment |
sed -e '/^select/,/^from/s/,/||/' temp.txt
Rakesh, when I am executing the command given by you, its replacing all commas present in the file. Wanted to replace the comma between Select and From keyword only.
– Ashish Mri
Mar 30 '17 at 2:39
add a comment |
sed -e '/^select/,/^from/s/,/||/' temp.txt
sed -e '/^select/,/^from/s/,/||/' temp.txt
answered Mar 29 '17 at 16:07
user218374
Rakesh, when I am executing the command given by you, its replacing all commas present in the file. Wanted to replace the comma between Select and From keyword only.
– Ashish Mri
Mar 30 '17 at 2:39
add a comment |
Rakesh, when I am executing the command given by you, its replacing all commas present in the file. Wanted to replace the comma between Select and From keyword only.
– Ashish Mri
Mar 30 '17 at 2:39
Rakesh, when I am executing the command given by you, its replacing all commas present in the file. Wanted to replace the comma between Select and From keyword only.
– Ashish Mri
Mar 30 '17 at 2:39
Rakesh, when I am executing the command given by you, its replacing all commas present in the file. Wanted to replace the comma between Select and From keyword only.
– Ashish Mri
Mar 30 '17 at 2:39
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f354600%2fusing-sed-how-to-replace-comma-before-a-specific-string%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
@Rakesh Sharma command edited sed -e '/^select/,/loc/s/,/||/'
– c4f4t0r
Mar 30 '17 at 9:34