delete string in a text file leaving the first occurrence


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers delete string in a text file leaving the first occurrence
# 1  
Old 10-27-2006
delete string in a text file leaving the first occurrence

Hi,

How can i delete the second and subsequent occurrence of a particular string from a file ?

eg) test.txt
cattle
bat
battle
mat
matter
cattle
cattle

my output file should be

cattle
bat
battle
mat
matter

I am new to unix and your advice is greatly appreciated.

Thanks in advance,
gops
# 2  
Old 10-27-2006
Looks like you want to remove the duplicate entries.

Removing duplicates
# 3  
Old 10-27-2006
Hi Vino,

Thanks for your prompt response.

Here i want to pass a string and only duplicate entry of that string should be deleted rather than all duplicates.

In the extract file which we have, lots of duplicates are available and we dont want to remove everything except the string which we specify explicitely.

Thanks in advance.

cheers,
gops
# 4  
Old 10-27-2006
Code:
#!/bin/ksh

awk -v check_val="$1" '{
        if( $0==check_val) { key[$0]++ }
        if(key[$0} < 2 ) print $0
       }' inputfile > outputfile

# 5  
Old 10-27-2006
Hi Jim,

I have a problem executing the awk script which you have recommended.

I am using Korn shell. Do i need to change some thing to accommodate that.
Herewith i am attaching my file

test.txt
cat
cattle
bat
battle
mat
matter
fat
fatter
cattle
cattle

I am bit confused about the script. When i execute it, I am getting following error message.

awk: syntax error near line 1
awk: bailing out near line 1

Let me know what i should assign for $0 and $1.
Thanks in advance.
Since I am a novice, I have several queries from my end.

cheers,
gops
# 6  
Old 10-27-2006
$1 is the string whose duplicates will be removed. Either you pass the string to that script or replace $0 by the string in that script.

Awk process the text file line by line. For each line read is placed in $0.
# 7  
Old 10-27-2006
Could you use 'uniq'?

Code:
uniq -u test.txt
cat
cattle
bat
battle
mat
matter
fat
fatter

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to delete identical lines while leaving one undeleted?

Hi, I have a file as follows. file1 Hello Hi His Hi Hi Hungry hi so I want to delete identical lines while leaving one of them undeleted. So desired output will be Hello Hi (2 Replies)
Discussion started by: beginner_99
2 Replies

2. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

3. Shell Programming and Scripting

Count number of occurrence of a string in file

if there's a file containing: money king money queen money cat money also money king all those strings are on one line in the file. how can i find out how many times "money king" shows up in the line? egrep -c "money king" wont work. (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi, I want to add a text to the end of the specific line in a file. Now my file looks like this: 999 111 222 333 111 444 I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The... (8 Replies)
Discussion started by: wenclu
8 Replies

5. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

6. Shell Programming and Scripting

Extracting text between two patterns 1 and 2 and pattern2 should be second occurrence of the file

Hi All, I have a small query. I have a file containing the following lines File 1: 29-Jul-2011 GMT Static data requires update <Extraction should start here> ----------- ----------- -------------------- ----------------------- ----------- <should stop here> Pattern1 will be time... (2 Replies)
Discussion started by: gangii87
2 Replies

7. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

8. UNIX for Dummies Questions & Answers

Delete all rows that contain a specific string (text)

Hi, I have a text file and I want to delete all rows that contain a particular string of characters. How do I go about doing that? Thanks! (9 Replies)
Discussion started by: evelibertine
9 Replies

9. UNIX for Dummies Questions & Answers

Delete all rows but leaving first and last ones

Hello, Merry Christmas to all! I wish you the best for these holidays and the best for the next year 2011. I'd like your help please, I need to delete all the rows in the third column of my file, but without touching nor changing the first and last value position, this is an example of my... (2 Replies)
Discussion started by: Gery
2 Replies

10. Shell Programming and Scripting

sed/awk: Delete matching words leaving only the first instance

I have an input text that looks like this (comes already sorted): on Caturday 22 at 10:15, some event on Caturday 22 at 10:15, some other event on Caturday 22 at 21:30, even more events on Funday 23 at 11:00, yet another event I need to delete all the matching words between the lines, from... (2 Replies)
Discussion started by: GrinningArmor
2 Replies
Login or Register to Ask a Question