Solved

Regular Expression Question

Posted on 2011-09-15
9
335 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:xoxomos
  • 4
  • 4
9 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36546299
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 '^$'
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36546324
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
0
 

Author Comment

by:xoxomos
ID: 36546343
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";
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Terry Woods
ID: 36546353
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:]]*$';
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 36546356
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

0
 

Author Comment

by:xoxomos
ID: 36546358
In the original question http://www.experts-exchange.com/OS/Unix/Q_27308970.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. :-(
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
ID: 36546366
The pattern ^$ matches only when the start of line (^) is next to the end of the line ($). If there's a space in between, it doesn't match.
0
 

Author Comment

by:xoxomos
ID: 36546371

This does not get rid of blank lines either in this particular file anyhow.
The solution arnold gave me does work however.
http://www.experts-exchange.com/OS/Unix/Q_27308970.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";
0
 

Author Closing Comment

by:xoxomos
ID: 36546400
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";$
 $
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question