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.

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
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
0
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.