Solved

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

Posted on 2008-06-22
3
225 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
Comment Utility

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

Open in new window

0
 
LVL 14

Assisted Solution

by:agriesser
agriesser earned 83 total points
Comment Utility
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:Kdo
Kdo earned 83 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now