Solved

bash remove multiple instances of text from file

Posted on 2006-06-14
4
270 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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,…
Fine Tune your automatic Updates for Ubuntu / Debian
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.:
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…

864 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

23 Experts available now in Live!

Get 1:1 Help Now