Solved

bash remove multiple instances of text from file

Posted on 2006-06-14
4
276 Views
Last Modified: 2008-03-06
I want to remove text from a file but only if it occurs more than once in that file

Any ideas

Thanks


0
Comment
Question by:jculkincys
[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
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:xDamox
ID: 16906297
Hi,

You could try this:

if [ `grep -c keyword target.txt` -gt 1 ]; then `cat target.txt | sed -e 's/keyword/newkeyword/g' >> newfile.txt`; fi

You will need to reaplce the keyword with the word you are looking for target.txt is the file your searching through
newkeyword is what you want to replace the old keyword with and it will create a file called newfile.txt with the
alterations in.
0
 
LVL 2

Author Comment

by:jculkincys
ID: 16906504
Cool

If I didn't want to create a new file could I do

if [ `grep -c keyword target.txt` -gt 1 ]; then `cat target.txt | sed -ie 's/keyword/newkeyword/g'`; fi



0
 
LVL 16

Accepted Solution

by:
xDamox earned 500 total points
ID: 16906968
Hi,

Almost you would do:

if [ `grep -c keyword target.txt` -gt 1 ]; then echo `cat target.txt | sed -e 's/keyword/newkeyword/g'`; fi

there is no i needed in the sed command also needed an echo :)
0
 
LVL 2

Author Comment

by:jculkincys
ID: 16907178
Thanks
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

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…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

729 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