Solved

bash remove multiple instances of text from file

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
issue in getting eth0 IP in oracale virtual box Linux VM 4 51
networking details on centos 6.6 4 62
Linux Copy Command - All Files inc Directory 1 42
linux 13 47
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é.
Fine Tune your automatic Updates for Ubuntu / Debian
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

786 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