Solved

bash remove multiple instances of text from file

Posted on 2006-06-14
4
269 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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…

763 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

10 Experts available now in Live!

Get 1:1 Help Now