How to edit file to have one line entry?

Hello All,

My file content is:

DROP TABLE
     "FACT_WORLD";
 
CREATE TABLE "FACT_WORLD" (
              "AR_ID" INTEGER NOT NULL,
              "ORG_ID" INTEGER NOT NULL
              )
      DATA CAPTURE NONE
      COMPRESS YES;

I want to change this file to have entries in one line only, how to do that using sed or awk? The new file should have entries like this-

DROP TABLE "FACT_WORLD";
 
CREATE TABLE "FACT_WORLD" ("AR_ID" INTEGER NOT NULL, "ORG_ID" INTEGER NOT NULL) DATA CAPTURE NONE COMPRESS YES;

Thanks,
Akash

Try this:

 tr '\n' ' ' < filename | sed -e 's/;/;\n/g' -e 's/[[:blank:]][[:blank:]]*/ /g'
sed -n 'H;$g;s/\n//g;s/;/;\n/g;s/  */ /g;$p' file
 awk 'BEGIN{RS=";"; ORS=";\n"}$1=$1'  yourSQL.sql

@sk1418
I like your awk: but what does the $1=$1 mean? Set the 1st column to the 1st column?

once the value of any field was reset, in this case $1 was reset by same value, awk will re-organize the whole line with default OFS=" ".

also the $1=$1 here will filter the empty line (after the last ";") out.

the example below may explain more clearly:


kent$ cat t.txt
aaa
bbb;
ccc
ddd;
eee
fff;

print the line directly:
kent$ awk 'BEGIN{RS=";";ORS=";\n"}{print $0}' t.txt
aaa
bbb;

ccc
ddd;

eee
fff;

;

$1 was reset, output lines look good, but the empty line is still there
kent$ awk 'BEGIN{RS=";";ORS=";\n"}{$1=$1;print $0}' t.txt
aaa bbb;
ccc ddd;
eee fff;
;

ok, now the empty line was filtered out with if statement
kent$ awk 'BEGIN{RS=";";ORS=";\n"}{if($1=$1) print $0}' t.txt
aaa bbb;
ccc ddd;
eee fff;

so if write it in short:
kent$ awk 'BEGIN{RS=";";ORS=";\n"}$1=$1' t.txt               
aaa bbb;
ccc ddd;
eee fff;

1 Like

Thanks, that is pretty nifty...so the test

($1=$1)

will fail where no fields exist in that record, thus no print.