0

I have in my PHP file this string :

$sql  = "SELECT p.pac_rut a, ";
$sql .= "       p.pac_dig b, ";
$sql .= "       p.pac_apepat c, ";
$sql .= "       p.pac_apemat d, ";
$sql .= "       p.pac_nombre e, ";
$sql .= "       to_char(p.pac_fecha_nac,'dd/mm/yyyy') as f, ";
$sql .= "       (to_char(sysdate,'yyyy')-to_char(p.pac_fecha_nac,'yyyy')) as g, ";
$sql .= "       p.sex_cod h ";
$sql .= "FROM   mytable ";
$sql .= "WHERE p.pac_rut=$rut";

And I want to transform it to one line string, like :

$sql  = "SELECT p.pac_rut a, p.pac_dig b, p.pac_apepat c, p.pac_apemat d, p.pac_nombre e, to_char(p.pac_fecha_nac,'dd/mm/yyyy') as f, (to_char(sysdate,'yyyy')-to_char(p.pac_fecha_nac,'yyyy')) as g, p.sex_cod h FROM   salud.pacientes p WHERE p.pac_rut=$rut";

How can I do that with sed or awk or with another UNIX tool ?

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
jpleouf
  • 1
  • 1
  • Do you want to transform the source file or the output of the source file? If it's the first, then this is fairly complicated, and probably not something you want to do with regular expressions... – Martin Tournoij Aug 11 '14 at 13:17

2 Answers2

0

Try this:

sed ':a;N;$!ba;s/\n/ /g' | sed 's/"; $sql .= "//g'

First sed command is used for replace new line character '\n'. How to replace new line character with sed I found here: How can I replace a newline (\n) using sed?

Hope this helps.

Community
  • 1
  • 1
djm.im
  • 3,295
  • 4
  • 30
  • 45
  • djjolem, now i need to replace all PHP variable por a bind variable. For ejample, replace the $rut variable by :rut. how can i do that without doing sed 's/$rut/:rut/g ? i need that this code find dynamically all the PHP variable and replace it. – jpleouf Aug 11 '14 at 13:37
  • @jpleouf I'm not shure how to do that - maybe, you should post another queston. If you do that post link to the question in comment and I will try to help. – djm.im Aug 11 '14 at 14:10
0

If you paste your code into file.txt you can make it happen like that:

sed -r 's/\$sql\s*\.?=\s*"(.*)";\s*/\1/' file.txt | sed -r 's/^\s+//' | tr '\n' ' ' | sed -r 's/(.*)/$sql = "\1"/'

It outputs:

$sql = "SELECT p.pac_rut a,  p.pac_dig b,  p.pac_apepat c,  p.pac_apemat d,  p.pac_nombre e,  to_char(p.pac_fecha_nac,'dd/mm/yyyy') as f,  (to_char(sysdate,'yyyy')-to_char(p.pac_fecha_nac,'yyyy')) as g,  p.sex_cod h  FROM   mytable  "

But I think what you really want to do is parse PHP files search for lines like $sql =... and replace them with one line version. To achieve this you should write script/program that:

  1. parse PHP files line by line
  2. search for lines starting with ^\s*\$sql\s*\.?=\s*"(.*)".*$ (not tetsted)
  3. remember position of all matched lines,
  4. get sql code from all matching lines
  5. join sql code from sequence of matching lines into one line
  6. replaces first line in sequence with joined sql code
  7. remove other lines in sequence
  8. repeat for other sequences

So if matched lines are 5, 6, 7, 11, 12, 13, squences are 5, 6, 7 and 11, 12, 13

piotrekkr
  • 2,785
  • 2
  • 21
  • 35
  • Piotrekkr, do you know how can i do this : -> replace all PHP variable by a bind variable ? For ejample, if i have $rut i need to replace it by ':rut'. if i have $proc i need to replace it by ':proc'. i need to do that dynamically. – jpleouf Aug 11 '14 at 13:50
  • @jpleouf Post it as another question and mark reply as solved if it solved your problem. – piotrekkr Aug 11 '14 at 14:03