?
Solved

bash remove multiple instances of text from file

Posted on 2006-06-14
4
Medium Priority
?
280 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 2000 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

800 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