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
# 8  
Old 10-27-2006
Hi Thomas,

I want to remove duplicates which are specified explicitely rather than all duplicates.
Thanks Thomas.

Hi Anbu,

I am getting the same error message. Can you take up my example and change the script accordingly.
Thanks in advance.

gops
# 9  
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

# 10  
Old 10-31-2006
Hi Anbu,

I was off for few days and hence the delay in replying.
I tried your stuff but I am getting this error
awk -v check_val="$1" '{
if( $0==check_val) { key[$0]++ }
if(key[$0] < 2 ) print $0
}' inputfile > outputfile

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

I created a script test and fired it like ksh test.ksh cattle

I am using korn shell. Do i need to make some changes for that ?

Thanks in advance.

cheers,
gops
# 11  
Old 10-31-2006
Hi Guys,

It is working fine when i supstitute nawk with awk command. Anbu, thanks a lot.

When i pass the exact string that line gets deleted in the outputfile.

But i want to pass a part of the string and i want whole string to be deleted.

for eg

inputfile

cattle
battle
cat
mattle
cattle

my outputfile should be
cattle
battle
mattle

here i want to check for cat%. I dont know how to do that using nawk.
Please help me out on this.

Best Regards,
gops
# 12  
Old 10-31-2006
Quote:
Originally Posted by anbu23
Code:
#!/bin/ksh

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

umm..how abt this?

awk '/$1/' inputfile>temp

awk '{for ( $0 in `cat temp` )
key[$0]++
if(key[$0] < 2 ) print $0
}' inputfile > outputfile


i didnt try this out, but i hope the logic is clear. i searched for the pattern specified by $1 and stored all the matches in temp. then instead of a direct comparison with 'if', i matched $0 with all the values in temp using a for loop. rest of the logic is the same. pardon any syntactical errors.
# 13  
Old 10-31-2006
Hi npolayan,

I can able to create the temp file with appropriate values to look at but when i executed the awk statement
awk '{ for ($0 in `cat temp`)
key[$0]++
if(key[$0] < 2 ) print $0
}' inputfile > outputfile

I am getting error messages

illegal statement at source line 1
illegal statement at source line 4

Let me know if you have the answer.

Thanks in advance.

cheers,
gops
 
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