Solved

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

Posted on 2008-06-22
3
231 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
[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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Utilizing an array to gracefully append to a list of EmailAddresses
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.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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