0

I am parsing a sql script using unix. If FROM is a first word then merge it with previous line. If FROM is last word in line then we need to merge it with next line. E.g.:

A
FROM 
B

I want the result as

A FROM B 

avoid any space and tabs.

Code:

cat A.txt | sed ':a;N;$!ba;s|[Ff][Rr][Oo][Mm][\s\t]*\n|FROM |g;s/\n\s*\t*[Ff][Rr][Oo][Mm]/ FROM/g' >B.txt
tripleee
  • 175,061
  • 34
  • 275
  • 318
Shikha S
  • 1
  • 2
  • 1
    Please show the code you got by now – Murmel Jan 15 '18 at 07:50
  • Do they end properly in `;`? – James Brown Jan 15 '18 at 08:22
  • I basically want to get source table name from sql query. For that I am taking next word after 'from'. Which is working fine. But, in some cases, 'From' is the last word in the line and tablename is next line. So, I want to remove new line after 'from'. code i am using : cat A.txt | sed ':a;N;$!ba;s|[Ff][Rr][Oo][Mm][\s\t]*\n|FROM |g;s/\n\s*\t*[Ff][Rr][Oo][Mm]/ FROM/g' >B.txt – Shikha S Jan 15 '18 at 08:50
  • Regardless of the precise solution, you want to avoid the [useless use of `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat) – tripleee Jan 15 '18 at 09:27

1 Answers1

0

Here is one using GNU awk and gensub. It replaces combination of spaces, newlines and tabs (carriage return omitted due to unix tag) before and after the word FROM. It uses empty RS as record separator, meaning that a record ends in an empty line or the end of file.

$ awk 'BEGIN{RS=""}{$0=gensub(/[ \t\n]+(FROM)[ \t\n]+/," \\1 ","g")}1' file
A FROM B

If you just want the word that comes after FROM:

$ awk 'BEGIN{RS=""}{for(i=1;i<=NF;i++)if($i=="FROM")print $(i+1)}' file
B

Both will fail if your query has FROM in the WHERE part values, like:

SELECT * FROM table WHERE variable='DEATH COMES FROM ABOVE';
James Brown
  • 36,089
  • 7
  • 43
  • 59
  • Thank you for your response. I am separating where clause in another file and then concatenating it after my task to avoid this problem. – Shikha S Jan 20 '18 at 20:00