Code_rocks Code_rocks - 5 months ago 7
Bash Question

Merge all the data within the (.....) in one line in shell script

I am new to shell script i need some help i have one SQL file like

SELECT DISTINCT F1.COL1,
F1.COL5 ADDRESS ,
COALESCE(COL1,
COL2,
COL3,
COL4),
F1.COL7
FROM TABLE1 F1


I need to print this in one line like

SELECT DISTINCT F1.COL1,
F1.COL5 ADDRESS ,
COALESCE(COL1,COL2,COL3,COL4),
F1.COL7
FROM TABLE1 F1


Thanks

Answer

With sed :

sed '/(/{:a;N;s/^ *//;s/\n *//;/)/!{ba}}' file

To edit file in place, add the -i option :

sed -i '/(/{:a;N;s/^ *//;s/\n *//;/)/!{ba}}' file

All lines starting with ( are joined until next line containing ).