Removing Duplicate Variables : SED?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing Duplicate Variables : SED?
# 1  
Old 10-14-2011
Removing Duplicate Variables : SED?

I have a file that needs to index and remove all duplicate variables (keeping the first of the duplicated). Then in another file, from the index I need to find and replace all those duplicated variables we deleted with their primary variable (the one we kept). I think this is a SED question, but if there are any better solutions I would greatly appreciate it.

Example:
Code:
newmtl m2
Kd 1.000000 1.000000 1.000000;
Ka 0 0 0
illum 2
Ns 64
d 1.000000
map_Kd 951E01Other4040.bmp

newmtl m3
Kd 1.000000 1.000000 1.000000;
Ka 0 0 0
illum 2
Ns 64
d 1.000000
map_Kd 951E01Other4040.bmp

newmtl m4
Kd 1.000000 1.000000 1.000000;
Ka 0 0 0
illum 2
Ns 64
d 1.000000
map_Kd 951E01Other4040.bmp

newmtl m5
Kd 1.000000 1.000000 1.000000;
Ka 0 0 0
illum 2
Ns 64
d 1.000000
map_Kd 951E01Other4040.bmp

Notice how m2, m3, m4, and m5 are the same. I need to delete m3, m4 and m5 and keep m2. Then in another file I have I need to find all variables m3, m4 and m5 and replace them with m2 because they no longer exist.

newmtl mXXX is the name and map_Kd is what determines if it is a duplicate. In this case we have map_Kd 951E01Other4040.bmp in others we have map_Kd A1C039w20h200x1CCOLORS.bmp and over 20 other meanings for map_Kd.

Hope I explained that in enough understandable detail. Let me know your thoughts.
Thank you! Smilie
# 2  
Old 10-14-2011
for starters:
Code:
$ nawk '!a[$NF]++' RS='' myFile
newmtl m2
Kd 1.000000 1.000000 1.000000;
Ka 0 0 0
illum 2
Ns 64
d 1.000000
map_Kd 951E01Other4040.bmp

# 3  
Old 10-14-2011
I am not familiar with this. How do I use this and what will it do?
# 4  
Old 10-14-2011
Quote:
Originally Posted by Blue Solo
I am not familiar with this. How do I use this and what will it do?
run it posted and see the output (similar to what's been posted).
If 'nawk' is not available, use either awk or gawk.
# 5  
Old 10-14-2011
It is running each line as a command:

Code:
MacBook-Pro:~ user$ newmtl m2
-bash: newmtl: command not found
MacBook-Pro:~ user$ Kd 1.000000 1.000000 1.000000;
-bash: Kd: command not found
MacBook-Pro:~ user$ Ka 0 0 0
-bash: Ka: command not found
MacBook-Pro:~ user$ illum 2
-bash: illum: command not found
MacBook-Pro:~ user$ Ns 64
-bash: Ns: command not found
MacBook-Pro:~ user$ d 1.000000
-bash: d: command not found
MacBook-Pro:~ user$ map_Kd 951E01Other4040.bmp

# 6  
Old 10-14-2011
Quote:
Originally Posted by Blue Solo
It is running each line as a command:

Code:
MacBook-Pro:~ user$ newmtl m2
-bash: newmtl: command not found
MacBook-Pro:~ user$ Kd 1.000000 1.000000 1.000000;
-bash: Kd: command not found
MacBook-Pro:~ user$ Ka 0 0 0
-bash: Ka: command not found
MacBook-Pro:~ user$ illum 2
-bash: illum: command not found
MacBook-Pro:~ user$ Ns 64
-bash: Ns: command not found
MacBook-Pro:~ user$ d 1.000000
-bash: d: command not found
MacBook-Pro:~ user$ map_Kd 951E01Other4040.bmp

hmmmm.... Show exactly what/how are you're running, please!
Follow the directions posted previously!
# 7  
Old 10-14-2011
I opened terminal.app and pasted:

Code:
awk '!a[$NF]++' RS='' /Users/user/Desktop/LevelIndices2.mtl 
newmtl m2 
Kd 1.000000 1.000000 1.000000; 
Ka 0 0 0 illum 2 
Ns 64 
d 1.000000 
map_Kd 951E01Other4040.bmp

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing duplicate items from an array

Hello Experts, I am trying to write a shell script to find duplicate items in an array, this is what i have tried : #!/bin/bash echo "This is another sample Progg to remove duplicate items from an array" echo "How many number do you want to insert" read n for (( i=0; i<$n; i++ )) do ... (5 Replies)
Discussion started by: mukulverma2408
5 Replies

2. Shell Programming and Scripting

Removing Duplicate Rows in a file

Hello I have a file with contents like this... Part1 Field2 Field3 Field4 (line1) Part2 Field2 Field3 Field4 (line2) Part3 Field2 Field3 Field4 (line3) Part1 Field2 Field3 Field4 (line4) Part4 Field2 Field3 Field4 (line5) Part5 Field2 Field3 Field4 (line6) Part2 Field2 Field3 Field4... (7 Replies)
Discussion started by: ekbaazigar
7 Replies

3. Shell Programming and Scripting

Removing duplicate terms in a file

Hi everybody I have a .txt file that contains some assembly code for optimizing it i need to remove some replicated parts. for example I have:e_li r0,-1 e_li r25,-1 e_lis r25,0000 add r31, r31 ,r0 e_li r28,-1 e_lis r28,0000 add r31, r31 ,r0 e_li r28,-1 ... (3 Replies)
Discussion started by: Behrouzx77
3 Replies

4. Post Here to Contact Site Administrators and Moderators

Removing or Merging some duplicate threads

I have made some threads that were identical and were about the same question :( I've made them in 3 forums , the moderator has moved and merged one of these threads. There is one thread left and it need to be merged or deleted. Is there any way I can delete it or merge it myself ? I have delete... (1 Reply)
Discussion started by: k.a.docpp
1 Replies

5. Shell Programming and Scripting

Removing duplicate records from 2 files

Can anyone help me to removing duplicate records from 2 separate files in UNIX? Please find the sample records for both the files cat Monday.dat 3FAHP0JA1AR319226MOHMED ATEK 966504453742 SAU2010DE 3LNHL2GC6AR636361HEA DEUK CHOI 821057314531 KOR2010LE 3MEHM0JG7AR652083MUTLAB NAL-NAFISAH... (4 Replies)
Discussion started by: zooby
4 Replies

6. Shell Programming and Scripting

Removing Duplicate Lines per Section

Hello, I am in need of removing duplicate lines from within a file per section. File: ABC1 012345 header ABC2 7890-000 ABC3 012345 Header Table ABC4 ABC5 593.0000 587.4800 ABC5 593.5000 587.6580 <= dup need to remove ABC5 593.5000 ... (5 Replies)
Discussion started by: petersf
5 Replies

7. Shell Programming and Scripting

removing the duplicate lines in a file

Hi, I need to concatenate three files in to one destination file.In this if some duplicate data occurs it should be deleted. eg: file1: ----- data1 value1 data2 value2 data3 value3 file2: ----- data1 value1 data4 value4 data5 value5 file3: ----- data1 value1 data4 value4 (3 Replies)
Discussion started by: Sharmila_P
3 Replies

8. Shell Programming and Scripting

removing duplicate blank lines

Hi, how to remove the blank lines from the file only If we have more than one blank line. thanks rameez (8 Replies)
Discussion started by: rameezrajas
8 Replies

9. UNIX for Dummies Questions & Answers

removing duplicate lines from a file

Hi, I am trying to remove duplicate lines from a file. For example the contents of example.txt is: this is a test 2342 this is a test 34343 this is a test 43434 and i want to remove the "this is a test" lines only and end up with the numbers in the file, that is, end up with: 2342... (4 Replies)
Discussion started by: ocelot
4 Replies

10. UNIX for Dummies Questions & Answers

Removing duplicate lines ignore case

hi, I have the following input in file: abc ab a AB b c a C B When I use uniq -u file,the out put file is: abc ab AB c v B C (17 Replies)
Discussion started by: hellsd
17 Replies
Login or Register to Ask a Question