?
Solved

Regular Expression Question

Posted on 2011-09-15
9
Medium Priority
?
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 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