I am trying to use sed (GNU sed version 4.2.1) to insert a line before each line in a file with the contents of the line surrounded by the line.
Input:
truncate table ALPHA; truncate table BETA; delete from TABLE_CHARLIE where ID=1;
Expected Result:
SELECT 'truncate table ALPHA;' from dual; truncate table ALPHA; SELECT 'truncate table BETA;' from dual; truncate table BETA; SELECT 'delete from TABLE_CHARLIE where ID=1;' from dual; delete from TABLE_CHARLIE where ID=1;
I tried using the special ampersand character (&), but this does not seem to work. If I put something after the ampersand on the replacement string, the result will be wrong.
Attempt 1:
sed -e "s/\(.*\)/SELECT '&\n&/g" input.txt output: SELECT 'truncate table ALPHA; truncate table ALPHA; SELECT 'truncate table BETA; truncate table BETA; SELECT 'delete from TABLE_CHARLIE where ID=1; delete from TABLE_CHARLIE where ID=1;
With the previous code, I get SELECT ' as expected, but as soon as I try to add ' from dual; to the right of the line, everything will fail.
Attempt 2:
sed -e "s/\(.*\)/SELECT '&' from dual;\n&/g" input.txt output: ' from dual;cate table ALPHA; truncate table ALPHA; ' from dual;cate table BETA; truncate table BETA; SELECT 'delete from TABLE_CHARLIE where ID=1;' from dual;
source share