Solved

How can I use shell script to remove specific records from a flat file?

Posted on 2008-06-22
3
228 Views
Last Modified: 2008-06-24
Hi all,
Working on a little shell script to remove specific records from a flat file. I have scanned a one or two script books and googled for answers but have no success. Can someone show me how I can do this. For example, how can I remove the something like the following records from among 200 other records:
DB2ADMIN.EPISODE_1;
DB2ADMIN.EPISODE_2;
DB2ADMIN.EPISODE_3;
DB2ADMIN.EPISODE_4;
DB2ADMIN.EPISODE_5;
DB2ADMIN.EPISODE_6;
DB2ADMIN.EPISODE_7;

You see, they are not duplicates, so uniq will not do the job. Is there another keyword that can remove something lke these given a string such as '  DB2ADMIN.EPISODE_%' in a shell script?
I'll truly appreciate any help I can get on this...

Thanks
0
Comment
Question by:Enuda
3 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 84 total points
ID: 21840132

egrep -v "DB2ADMIN.EPISODE_?" file >newfile

Open in new window

0
 
LVL 14

Assisted Solution

by:agriesser
agriesser earned 83 total points
ID: 21840890
I usually use perl for such things as the default shell utils are not able to replace contents of files without writing new ones as shown in the example from Tintin.

The perl one-liner for this would be:
perl -p -i -e 's/^DB2ADMIN\.EPISODE_[0-9]+;\n$//' yourfile

Open in new window

0
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 83 total points
ID: 21849804
And the sed equivalent would be:

  sed '/DB2ADMIN.EPISODE_/d' yourfile > newfile

or, if 'EPISODE_' must be followed by a digit:

  sed '/DB2ADMIN.EPISODE_[0-9]/d' yourfile > newfile


Lots of ways to do things in unix.   :)

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

821 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