[SOLVED] How to remove duplicate words within a particular text in a file?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to remove duplicate words within a particular text in a file?
I am basically trying to remove duplicate words in my <title></title> tag after I got hit by Google Panda. I have around 750 .html files and it will be difficult for to me remove one by one. I am looking for a way to remove only from within <title> </title>
Example of a duplicate title I have:
Code:
<title>Pasta, Pasta Recipe and Pasta Guide</title>
Can someone suggest me a way to do it? I dont want to replace those words anywhere else in the file except for within the <title>
Thanks for your reply. I basically just want Pasta to appear once in the <title> and wanted to remove the other two pasta reference in my title. My files have a similar pattern for other dishes too.
Actually I have 700 files of various dishes and which means Pasta was an example. Rest of the words remain same. I just want țo remove the dish names which I got them repeated.
Îs it possible to remove words from , till </title> ,
sed 's/<title>\([A-Z][a-z]*\), \1 \([A-Z][a-z]*\) and \1 \([A-Z][a-z]*\)<\/title>/<title>\1, \2 and \3<\/title>/g' file
Code:
sycamorex@mordor:~/temp$ cat file
<title>Pasta, Pasta Recipe and Pasta Guide</title>
<title>Soup, Soup Recipe and Soup Guide</title>
<title>Beans, Beans Recipe and Beans Guide</title>
<title>Cakes, Cakes Recipe and Cakes Guide</title>
sycamorex@mordor:~/temp$ sed 's/<title>\([A-Z][a-z]*\), \1 \([A-Z][a-z]*\) and \1 \([A-Z][a-z]*\)<\/title>/<title>\1, \2 and \3<\/title>/g' file
<title>Pasta, Recipe and Guide</title>
<title>Soup, Recipe and Guide</title>
<title>Beans, Recipe and Guide</title>
<title>Cakes, Recipe and Guide</title>
Actually I have 700 files of various dishes and which means Pasta was an example. Rest of the words remain same. I just want țo remove the dish names which I got them repeated.
Îs it possible to remove words from , till </title> ,
So for every HTML file, check if the title contains more than one instance of a certain word, and if so, remove all but the first instance of that word. Is that correct?
So for every HTML file, check if the title contains more than one instance of a certain word, and if so, remove all but the first instance of that word. Is that correct?
I am not sure that a quick and dirty one liner will do it. As you are parsing html it would probably be best to use something like Perl.
However, if the pattern were to be the same and the first word is always the one to be replaced and we don't have to worry about case sensitivity (ie it will always be the same).
Something like this might work:
sed -ri 's@<TITLE>([^,]+), \1 Recipe and \1 Guide</TITLE>@<TITLE>\1, Recipe and Guide</TITLE>@g' *.html
Also, you didn't mention the very important fact that the <TITLE> tags are uppercase (and note that it's preferred for HTML tags to be lowercase, and AFAIK newer versions of HTML will only support lowercase tags).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.