Using sed how to replace comma before a specific string












0















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.










share|improve this question














bumped to the homepage by Community 4 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






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
















0















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.










share|improve this question














bumped to the homepage by Community 4 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






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














0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 29 '17 at 14:31









Ashish MriAshish Mri

11




11





bumped to the homepage by Community 4 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 4 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






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



















  • @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










2 Answers
2






active

oldest

votes


















0














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');





share|improve this answer
























  • 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



















0














sed -e '/^select/,/^from/s/,/||/' temp.txt





share|improve this answer
























  • 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













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
});


}
});














draft saved

draft discarded


















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









0














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');





share|improve this answer
























  • 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
















0














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');





share|improve this answer
























  • 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














0












0








0







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');





share|improve this answer













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');






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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













0














sed -e '/^select/,/^from/s/,/||/' temp.txt





share|improve this answer
























  • 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


















0














sed -e '/^select/,/^from/s/,/||/' temp.txt





share|improve this answer
























  • 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
















0












0








0







sed -e '/^select/,/^from/s/,/||/' temp.txt





share|improve this answer













sed -e '/^select/,/^from/s/,/||/' temp.txt






share|improve this answer












share|improve this answer



share|improve this answer










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





















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Loup dans la culture

How to solve the problem of ntp “Unable to contact time server” from KDE?

ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux