To Delete the repeated occurances and print in same line by appending values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To Delete the repeated occurances and print in same line by appending values
# 8  
Old 06-26-2009
@ Panyam and @ Rakesh thanks a lot.... It did serve my purpose.... Thanks.. But i am stil working on it.. and will get back, as my original input file is different, so trying to impliment on it... I think it wil be better to have a delimiter to differentiate...

@ ghostdog74 ya da its a type error sorry for confusion

Smilie

---------- Post updated 06-26-09 at 10:10 AM ---------- Previous update was 06-25-09 at 05:59 PM ----------

Originally quoted by Rakesh:

----------------------------------------
/bin/sort inputfile | awk ' BEGIN { FS=OFS=" ";getline;prev=$1" "$2" "$3;s=$4 }

{
curr=$1" "$2" "$3
# print curr
if ( prev == curr )
s=s" "$4
if ( prev != curr )
{
print prev " " s
prev=curr;s=$4
}
}
END {
print prev " " s
}'
------------------------------------------

This piece of code worked fine but thr is one more thing i would like to highlight.. You have sirted entire inputfile hence the occurances are not printed in order they are see the example:

inputfile:
a b c 1
a b c 2
a b c 3
x y z 6
x y z 44
a b c 89
x y z 9
b s c 100
a b c 19

Output of above code:
a b c 1 19 2 3 89
b s c 100
x y z 44 6 9

So here al these values appended ones also sorted..
Instead shud get as below:

a b c 1 2 3 89 19
b s c 100
x y z 6 44 9

I dont mind the order of values if 1,2,3.. N then can sort only til N-1.. Bec last value is one with different values

Please help in this regard.
# 9  
Old 06-26-2009
In such case I will use paynam's code (i really liked that and learnt) with little modification...
Code:
sort -n -k4,4 inputfile | awk '{a[$1" "$2" "$3]=a[$1" "$2" "$3]" "$4}END{for (i in a) print i a[i]}'



---------- Post updated at 10:57 AM ---------- Previous update was at 10:56 AM ----------

i suggest you to produce your code while posting queries... you could have tried ...
man sort.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract values and print at end of each line

In the below perl I am trying to extract and print the values AF1=, the GT value, and F or QUAL diveded by 33 (rounded to the nearest whole #). The GT value is at the end after the GT:PL so all the possibilities are read into a hash h, then depending on the value that is in the line the... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Adding the values of repeated ids

File1 consist two columns, first some weired ids and second the numbers t|v203.1@t|k88711.1 0.1 t|v190.1@t|k90369.1 0.01 t|v203.1@t|k88711.1 0.5 t|v322.1@t|k88711.1 0.2 t|v207.1@t|k90369.1 0.11 t|v326.1@t|k85939.1 0.5 t|v207.1@t|k90369.1 0.7 t|v207.1@t|k90369.1 0.3 t|v326.1@t|k89421.1 0.33... (3 Replies)
Discussion started by: ashmit99
3 Replies

3. Shell Programming and Scripting

Print line if values in fields matches number and text

datafile: 2017-03-24 10:26:22.098566|5|'No Route for Sndr:RETEK RMS 00040 /ZZ Appl:PF Func:PD Txn:832 Group Cntr:None ISA CntlNr:None Ver:003050 '|'2'|'PFI'|'-'|'EAI_ED_DeleteAll'|'EAI_ED'|NULL|NULL|NULL|139050594|ActivityLog| 2017-03-27 02:50:02.028706|5|'No Route for... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

5. Shell Programming and Scripting

Reoccuring peak values in large data file and print the line..

Hi i have some large data files that contain several fields and rows the data in a field have a numeric value that is in a sine wave pattern what i would like todo is locate each peak and pick the highest value and print that complete line. the data looks something like this it is field nr4 which... (4 Replies)
Discussion started by: ninjaunx
4 Replies

6. Shell Programming and Scripting

Put repeated values into a table

Hi all, I have blocks of records like the following, each block ends in = in a new line, I want tabularize the entire output. The pattern is the same in every block although not all types are there in every block. For example gine3 is absent in the second block but present first and third. ... (7 Replies)
Discussion started by: ritakadm
7 Replies

7. Shell Programming and Scripting

Resume and count repeated values

Gents, Please can you help me. Input file 1050 , 9 ,9888 1050 ,10 ,9888 1050 ,11 ,9888 1050 ,13 ,9888 1050 ,15 ,9888 1051 , 9 ,9889 1051 ,12 ,9889 1051 ,15 ,9889 1051 ,18 ,9889 1052 , 9 ... (7 Replies)
Discussion started by: jiam912
7 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Print a line using a max and a min values of different columns

Hi guys, I already search on the forum but i can't solve this on my own. I have a lot of files like this: And i need to print the line with the maximum value in last column but if the value is the same (2 in this exemple for the 3 last lines) i need get the line with the minimum value in... (4 Replies)
Discussion started by: MetaBolic0
4 Replies

9. Shell Programming and Scripting

delete repeated strings (tags) in a line and concatenate corresponding words

Hello friends! Each line of my input file has this format: word<TAB>tag1<blankspace>lemma<TAB>tag2<blankspace>lemma ... <TAB>tag3<blankspace>lemma Of this file I need to eliminate all the repeated tags (of the same word) in a line, as in the example here below, but conserving both (all) the... (2 Replies)
Discussion started by: mjomba
2 Replies

10. UNIX for Dummies Questions & Answers

Delete repeated nos in a file

Hi, I need to delete repeated nos in a file and finally list the count. Can some one assist me? file: 12345 12345 56345 12345 23896 Output needed: 12345 56345 23896 Total count:3 Thanks (2 Replies)
Discussion started by: gini
2 Replies
Login or Register to Ask a Question