The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
using sed to get rid of duplicated columns... fedora Shell Programming and Scripting 5 11-28-2008 10:45 AM
Can root ID be duplicated lorcan UNIX for Advanced & Expert Users 6 06-16-2007 12:06 AM
grep and delete ONE only happyv Shell Programming and Scripting 10 01-16-2007 10:02 AM
checking jump sequence number (part2) happyv Shell Programming and Scripting 3 10-19-2006 07:03 AM
copy syntax part2 tomapam UNIX for Dummies Questions & Answers 3 07-02-2002 12:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-17-2007
happyv happyv is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 209
grep and delete 2nd duplicated of txt... -part2

Hi,

I find out one problem is...the main point is we must delete 2nd duplicated of word in txt file. For example

apple
orange
pink
green
orange
yellow
orange
red

output should be:
apple
orange
pink
green
yellow
orange
red

#For the above, check all words in txt file and delete the 2nd duplicated of word (and MUST not use sort to do, because of the order should not be changed - only delete the duplicated). Due to the file may be over 10000 words, it is very difficult for me to check..by manually.....

any small script can help?
  #2 (permalink)  
Old 01-17-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
What's wrong with the solution in this thread?

And your definition of duplicated is a bit confusing. To delete duplicate entries means that except the first entry, all other entries of that word/phrase will be deleted.

Another question. Is this any form of school work/home work?
  #3 (permalink)  
Old 01-17-2007
kapilraj kapilraj is offline
Registered User
  
 

Join Date: Dec 2006
Location: Maryland
Posts: 162
count=0
cat {1} | while read line
do
count=`echo "${count} + 1" | bc`
cc=`head -${count} ${1} |grep -c ${line}`
if [ ${cc} -eq 2 ]
then
:
else
echo ${line}
fi
done

Would this work ? I have not tested it but it should work. save it as a file , give it execute permission and then use it as "script filename".

All the best. It's a simple logic but i think that would work for you.

Regds,

Kaps
  #4 (permalink)  
Old 01-19-2007
happyv happyv is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 209
Quote:
Originally Posted by kapilraj
count=0
cat {1} | while read line
do
count=`echo "${count} + 1" | bc`
cc=`head -${count} ${1} |grep -c ${line}`
if [ ${cc} -eq 2 ]
then
:
else
echo ${line}
fi
done

Would this work ? I have not tested it but it should work. save it as a file , give it execute permission and then use it as "script filename".

All the best. It's a simple logic but i think that would work for you.

Regds,

Kaps
LOOK WOKING...BUT can I specify any word to delete? such as orange..and it can delete the 2nd duplicated of "orange" only.
  #5 (permalink)  
Old 01-19-2007
Krrishv Krrishv is offline
Registered User
  
 

Join Date: Dec 2006
Location: CA,United States
Posts: 186
chechout this script

#! /usr/bin/bash
cat $1|sort -c
if [ $? -eq 1 ]; then
sort -u $1|tee $1
else
echo "Already sorted"
fi

let me know if you have any questions. $1 is your input filename
  #6 (permalink)  
Old 01-19-2007
happyv happyv is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 209
Quote:
Originally Posted by Krrishv
chechout this script

#! /usr/bin/bash
cat $1|sort -c
if [ $? -eq 1 ]; then
sort -u $1|tee $1
else
echo "Already sorted"
fi

let me know if you have any questions. $1 is your input filename
thank you for the script..however, I cannot sort the txt file..because of all the word in the text file of order..should not be change. You can see my exmaple at the top of the post. I only need to delete specify (such as orange) word of 2nd duplicated and nothing change with other...
  #7 (permalink)  
Old 01-19-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Slight modification to Ygor script

Code:
awk -v str="orange" '++a[$0]!=2 || $0 != str' file

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0