grep and delete 2nd duplicated of txt... -part2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep and delete 2nd duplicated of txt... -part2
# 1  
Old 01-17-2007
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  
Old 01-17-2007
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  
Old 01-17-2007
This will remove only the second instance of duplicated lines....
Code:
awk '++a[$0]!=2' file1

# 4  
Old 01-17-2007
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
# 5  
Old 01-19-2007
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.
# 6  
Old 01-19-2007
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
# 7  
Old 01-19-2007
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete 'duplicated' column values and make a delimited file too?

Hi, I have the following output from an Oracle SQL statement and I want to remove duplicated column values. I know it is possible using Oracle analytical/statistical functions but unfortunately I don't know how to use any of those. So now, I've gone to PLAN B using awk/sed maybe or any... (5 Replies)
Discussion started by: newbie_01
5 Replies

2. UNIX for Dummies Questions & Answers

Delete files in a txt file

Hi, I have very old files in my server like from 2012 and i want to delete them, Please help. Thanks in advance.. (2 Replies)
Discussion started by: nanz143
2 Replies

3. Shell Programming and Scripting

Delete duplicated fields in a line

Hi, I have files with this kind of format (separator is space): A1 B1 C1 D1 E1 F1 D1 C1 G1 H1 A2 B2 C2 D2 E2 F2 D2 C2 G2 H2 A3 B3 C3 D3 E3 F3 G3 D3 C3 H3 A4 B4 C4 D4 E4 F4 G4 D4 C4 H4 I want the output to be: A1 B1 E1 F1 G1 H1 A2 B2 E2 F2 G2 H2 A3 B3 E3 F3 G3 H3 A4 B4 E4 F4 G4... (12 Replies)
Discussion started by: Gr4wk
12 Replies

4. Shell Programming and Scripting

Delete file2.txt from file1.txt using scripting

Hi, I`m a total newbie, well my requirement is that i have 2 files I want to identify which countries i do not currently have in db.. how can i use the grep or another command to find this file .. i want to match all-countries.txt with countries-in-db.txt so the output is equal to... (11 Replies)
Discussion started by: beanbaby
11 Replies

5. UNIX for Dummies Questions & Answers

If then else for decimal numbers part2

Hi, I have a small problem with my script. I have everything in order but it doesnt seem to compare anything less than 1 correctly. If the input is more than 1, then the results is correct. If the input is 0.xxx (anything) it returns erroneous results. Pls help input=0.12 if ; then ... (7 Replies)
Discussion started by: streddy
7 Replies

6. Homework & Coursework Questions

Delete first both lines from .txt

Hi, I have to programming a program in shell script which combine a lot of .txt files. But in all of these files the program had to delete the first both lines. Because I don't know anything about shell script I need your help. Does anyone have a command or a hint, where I can look for? ... (1 Reply)
Discussion started by: mkrol
1 Replies

7. Shell Programming and Scripting

Delete first and second lines from .txt

Hi, I have to programming a program in shell script which combine a lot of .txt files. But in all of these files the program had to delete the first both lines. Because I don't know anything about shell script I need your help. Does anyone have a command or a hint, where I can look for? ... (1 Reply)
Discussion started by: mkrol
1 Replies

8. Shell Programming and Scripting

delete duplicated characters in each line

I'm a biologist trying to analyse some data and I'll appreciate some help with the following problem. I have a column of characters which I'll like to delete the duplicated characters in each line and report only the unique one.No sorting should be done. E.g. The original data: GTG CTC CTC... (5 Replies)
Discussion started by: ivpz
5 Replies

9. Shell Programming and Scripting

sed to read x.txt and grep from y.txt

How would I write a command(s) to read from a file (list) that looks like this: 29847374384 and grep from a second file (list) that looks like this: 29847374384, jkdfkjdf,3833,ddd:confused: (1 Reply)
Discussion started by: smellylizzard
1 Replies

10. Windows & DOS: Issues & Discussions

sftp not get txt file 2nd time

I use sftp batch script to copy file from a remote server to local machine. Problem is when I rerun this shell after copying txt file it do not allow me to copy txt file again. My question is , is this confrigration problem of remote server. beace same shell work fie with the other remote... (3 Replies)
Discussion started by: Jamil Qadir
3 Replies
Login or Register to Ask a Question