place delimiter when found repeated occurance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting place delimiter when found repeated occurance
# 1  
Old 07-09-2009
place delimiter when found repeated occurance

Hi all,

Searched lot in the forum but couldnt find relative post to my requirement..Looking forward for the solution.

I am basically having a huge file with repeated digits which is getting tuff to track so would want to place a delimiter in between for better clarity..

Input

Code:
4 0 4 6 4 101 1011 57 70 64 88 119 64 88 119  37
4 0 4 6 4 101 1012 62 75 115 117 122 115 117 122  37
4 0 4 6 4 101 1013 59 68 112 124 112 124  37
4 0 4 6 4 101 1021 53 71 64 64 39
4 0 4 6 4 101 1022 54 73 115 115 39
4 0 4 6 4 101 1031 57 81 64 64 39
4 0 4 6 4 101 1032 57 77 64 64 49
4 0 4 6 4 101 1041 58 80 115 115 49
4 0 4 6 4 101 1042 59 67 112 124 120 112 124 120  49

Output

Code:
4 0 4 6 4 101 1011 57 70--64 88 119--64 88 119   37
4 0 4 6 4 101 1012 62 75--115 117 122---15 117 122  37
4 0 4 6 4 101 1013 59 68--112 124--112 124  37
4 0 4 6 4 101 1021 53 71--64--64 39
4 0 4 6 4 101 1022 54 73--115-115 39
4 0 4 6 4 101 1031 57 81--64--64 39
4 0 4 6 4 101 1032 57 77--64--64 49
4 0 4 6 4 101 1041 58 80--115--115 49
4 0 4 6 4 101 1042 59 67--112 124 120--112 124 120  49

Here delimiter is placed first after 9th position then next digit is considered and searched and placed before the found string.... If you see the data with in the delimiter is the repeated data which is found later...

Thanks in advance

Shalini
# 2  
Old 07-10-2009
Hi Shalini,
I went through your thread, and initially thought that this would be a very easy one. But when I started working on it, it really did take some time Smilie
What I thought should be a 4-5 line awk code turned out to be pretty big one!!! Here's what I could come up with-
Code:
awk '
{
printf("\n%s %s %s %s %s %s %s %s %s",  $1,$2,$3,$4,$5,$6,$7,$8,$9);
i=10;
while(i<=NF)
{
nexot=i+1;
for(j=i+1;j<=NF;j++)
{
if($i==$j)
{
nexot=j+1;
}
}
if(nexot>i+1)
{
printf("  - ");
for(k=i;k<nexot-1;k++)
printf("  %s",$k);
printf(" -  %s",$(nexot-1));
}
else
{
printf("  %s",$i);
}
i=nexot;
}
}' a

Yea, I know, you might be saying there's more of C code here than awk code, but this is the best I could come up with. It did work for me, let me know if it works for you too.
Oh yea, forgot to mention, the 'a' at the end of the code is not an unwanted character(as thought of by many of my friends), but the input file which contains the input as specified by you. Enjoy!!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

2. Shell Programming and Scripting

Find repeated word and take sum of the second field to it ,for all the repeated words in awk

Hi below is the input file, i need to find repeated words and sum up the values of it which is second field from the repeated work.Im trying but getting no where close to it.Kindly give me a hint on how to go about it Input fruits,apple,20,fruits,mango,20,veg,carrot,12,veg,raddish,30... (11 Replies)
Discussion started by: 100bees
11 Replies

3. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

4. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/43°13'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

5. Shell Programming and Scripting

remove anything after repeated string pattern found

HI, Can anyone help me with a script. i/p calc 1 2 3 4 5 6 7 8 calc 4 5 6 calc 7 8 9 o/p calc 1 2 3 4 5 6 7 8 calc 4 5 6 i.e remove anything after where two times the string calc is found. thanks (3 Replies)
Discussion started by: Indra2011
3 Replies

6. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

7. Solaris

What is the best way to copy data from place to another place?

Dear Gurus, I need you to advice or suggestion about the best solution to copy data around 200-300G from serverA(location A) to serverB(location B). Normally, I will share folder and then copy but it takes too long time(about 2 days). Do you have any suggestion or which way should be... (9 Replies)
Discussion started by: unitipon
9 Replies

8. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

9. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

10. Shell Programming and Scripting

how can i check in csh if command found or not found ?

hello all im trying to use in sun Solaris the information received from the top command now i several machines that dont have install the top program so when im running the script im geting error saying after im running this code : set MemoryInfo = `top | grep Memory` if (... (2 Replies)
Discussion started by: umen
2 Replies
Login or Register to Ask a Question