Link to home
Start Free TrialLog in
Avatar of xoxomos
xoxomos

asked on

Regular Expression Question

I was given this as a solution to a problem of needing to remove blank lines from a file.
cat create_user.sql | egrep -i '[a-z0-9\-]'
The way i'm understanding i should get everything a thru z and 0 thru 9 plus the underscore _.
I got all this plus quotes double and single ' " and semicolon ; all of which is fine.  I just don't understand how i got these characters in addition to the ones in the regular expression.
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

That command excludes lines without an alphabetic, numeric or hyphen character. It would exclude a line with only other characters, such as just ; on it for example.

If you want to exclude only blank lines, this looks good to me, using egrep:

cat create_user.sql |egrep -v '^$'
Or, a nice alternative technique:

perl -pe 's/^\s*$//' create_user.sql   #outputs the file (like cat) without blank lines

And if you're happy with the result, run:

perl -i.bak -pe 's/^\s*$//' create_user.sql   #copy the original with a .bak extension, then commit the change
Avatar of xoxomos
xoxomos

ASKER

Well yes and no.  Your explanation why cat create_user.sql | egrep -i '[a-z0-9\-]'   worked to eliminate blank lines sounds correct to me.

The alternative you gave to get rid of blank lines in the file does not work.

oracle@bb7b:/u05/datapump_directory$ cat create_user.sql | egrep -v '^$';
-- new object type path is: DATABASE_EXPORT/SYS_USER/USER
 ALTER USER "SYS" IDENTIFIED BY VALUES 'BBFEC2ABA14349B0'
      TEMPORARY TABLESPACE "TEMP";
 
-- new object type path is: DATABASE_EXPORT/SCHEMA/USER
 ALTER USER "SYSTEM" IDENTIFIED BY VALUES 'E3C232DAD21E66AE'
      DEFAULT TABLESPACE "TOOLS"
      TEMPORARY TABLESPACE "TEMP";
 
 CREATE USER "OUTLN" IDENTIFIED BY VALUES '4A3BA55E08595C81'
      TEMPORARY TABLESPACE "TEMP";
cat create_user.sql | egrep -v '^$';

will only work if there are no whitespace characters on the line. This worked for me with whitespace though:

cat create_user.sql | egrep -v '^[[:space:]]*$';
To filter empy lines and lines contain spaces or tabs and create a new file:
grep -v '^[[:space:]]*$' create_user.sql > create_user_clean.sql

Open in new window

Avatar of xoxomos

ASKER

In the original question https://www.experts-exchange.com/questions/27308970/Delete-Blank-Lines-From-A-File.html i had tried to use the ^$ pattern to get rid of blank lines and it did not work.  Still don't know why not. :-(
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xoxomos

ASKER


This does not get rid of blank lines either in this particular file anyhow.
The solution arnold gave me does work however.
https://www.experts-exchange.com/questions/27308970/Delete-Blank-Lines-From-A-File.html
oracle@bb7b:/u05/datapump_directory$ grep -v '^[[:space:]]*$' create_user.sql > create_user_clean.sql
oracle@bb7b:/u05/datapump_directory$ vi *clean*
"create_user_clean.sql" 88 lines, 3161 characters
-- new object type path is: DATABASE_EXPORT/SYS_USER/USER
 ALTER USER "SYS" IDENTIFIED BY VALUES 'BBFEC2ABA14349B0'
      TEMPORARY TABLESPACE "TEMP";

-- new object type path is: DATABASE_EXPORT/SCHEMA/USER
 ALTER USER "SYSTEM" IDENTIFIED BY VALUES 'E3C232DAD21E66AE'
      DEFAULT TABLESPACE "TOOLS"
      TEMPORARY TABLESPACE "TEMP";

 CREATE USER "OUTLN" IDENTIFIED BY VALUES '4A3BA55E08595C81'
      TEMPORARY TABLESPACE "TEMP";

 CREATE USER "TRACESVR" IDENTIFIED BY VALUES 'F9DA8977092B7B81'
      DEFAULT TABLESPACE "SMALL_DEFAULT"
      TEMPORARY TABLESPACE "TEMP";

 CREATE USER "BBADMIN" IDENTIFIED BY VALUES '35DE13A3ED9CDD95'
      DEFAULT TABLESPACE "BBADMIN_DATA"
      TEMPORARY TABLESPACE "TEMP";
Avatar of xoxomos

ASKER

the perl works too.

That sounds like it might be the explanation, but when I set list in vi, i don't see anything between the beginning and $

- new object type path is: DATABASE_EXPORT/SYS_USER/USER$
 ALTER USER "SYS" IDENTIFIED BY VALUES 'BBFEC2ABA14349B0'$
      TEMPORARY TABLESPACE "TEMP";$
 $
-- new object type path is: DATABASE_EXPORT/SCHEMA/USER$
 ALTER USER "SYSTEM" IDENTIFIED BY VALUES 'E3C232DAD21E66AE'$
      DEFAULT TABLESPACE "TOOLS"$
      TEMPORARY TABLESPACE "TEMP";$
 $
 CREATE USER "OUTLN" IDENTIFIED BY VALUES '4A3BA55E08595C81'$
      TEMPORARY TABLESPACE "TEMP";$
 $
 CREATE USER "TRACESVR" IDENTIFIED BY VALUES 'F9DA8977092B7B81'$
      DEFAULT TABLESPACE "SMALL_DEFAULT"$
      TEMPORARY TABLESPACE "TEMP";$
 $
 CREATE USER "BBADMIN" IDENTIFIED BY VALUES '35DE13A3ED9CDD95'$
      DEFAULT TABLESPACE "BBADMIN_DATA"$
      TEMPORARY TABLESPACE "TEMP";$
 $
 CREATE USER "BB_BB60_STATS" IDENTIFIED BY VALUES 'DD5B4DC833EE258E'$
      DEFAULT TABLESPACE "BB_BB60_STATS_DATA"$
      TEMPORARY TABLESPACE "TEMP";$
 $
 CREATE USER "BB_BB60" IDENTIFIED BY VALUES '7EFB15396EAF1168'$
      DEFAULT TABLESPACE "BB_BB60_DATA"$
      TEMPORARY TABLESPACE "TEMP";$
 $