Add similar pairs in a txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add similar pairs in a txt file
# 1  
Old 03-07-2013
Add similar pairs in a txt file

Hi guys!!!

In my txt file there are a lot of pairs. Some of them are similar, so I am trying to add these pairs. For a example:

File:
Code:
  ASP - GLN = 14
  SER - GLU = 14
  ARG - ASN = 13
  ARG - TYR = 13
  ASP - ARG = 13
  GLU - ARG = 13
  GLU - GLN = 13
  ALA - ARG = 12
  ARG - GLN = 12
  ASN - ARG = 12
  ASN - ASP = 12
....

I want the output file to add these similar pairs:

Output file:
Code:
  ASP - GLN = 14
  SER - GLU = 14
  ARG - ASN = 25
  ARG - TYR = 13
  ASP - ARG = 13
  GLU - ARG = 13
  GLU - GLN = 13
  ALA - ARG = 12
  ARG - GLN = 12
  ASN - ASP = 12
.....

Any solutions Smilie Thanks!!!
# 2  
Old 03-07-2013
Is your file fixed width? If so, a (rather clunky) script could do this:-
Code:
#!/bin/ksh

for refs in `cut -d"=" filename | sort -u`
do
   tot=0
   grep "$refs" filename | while read a b c d value
   do
      ((tot=$tot+$value))
   done
   print "$refs $tot"
done > newfile

Probaby much neater and more efficient with an awk, but I'm not proficient enough to write them.



I hope that this helps.

Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 03-07-2013
Hi rbatte1. Thanks for your reply. Unfortunately I am not familiar with ksh.
I try to use this script but I couldn't run it Smilie
# 4  
Old 03-07-2013
Code:
# cat foot
  ASP - GLN = 14
  SER - GLU = 14
  ARG - ASN = 13
  ARG - TYR = 13
  ASP - ARG = 13
  GLU - ARG = 13
  GLU - GLN = 13
  ALA - ARG = 12
  ARG - GLN = 12
  ASN - ARG = 12
  ASN - ASP = 12
# awk '{i=($3<$1?$3" - "$1:$1" - "$3);A[i]+=$5}END{for(i in A) print i" = " A[i]}' foot
ARG - TYR = 13
ARG - ASP = 13
ARG - GLU = 13
GLU - SER = 14
ALA - ARG = 12
ARG - GLN = 12
ASP - GLN = 14
ASN - ASP = 12
GLN - GLU = 13
ARG - ASN = 25
#

These 2 Users Gave Thanks to ctsgnb For This Post:
# 5  
Old 03-07-2013
ctsgnb exactly what I need Smilie Smilie Thanks!!!!!
# 6  
Old 03-07-2013
Nice method to find the key!
Good, the order doesn't matter!
An attempt to keep the original order makes it more complex and increases memory consumption.
Code:
awk '{i=($3<$1?$3" - "$1:$1" - "$3)} !(i in A) {B[NR]=i}  {A[i]+=$5}END{for(i=1;i in B;i++) print B[i]" = " A[B[i]]}'

# 7  
Old 03-07-2013
Another one that preserves order:
Code:
awk 'NR==FNR{if(T[$3,$1]x) T[$3,$1]+=$5; else T[$1,$3]+=$5; next} $5=T[$1,$3]x' file file


Last edited by Scrutinizer; 03-07-2013 at 03:08 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can i add each line from a txt file to different files in the same directory?

Hello, this is my first thread here :) So i have a text file that contains words in each line like abcd efgh ijkl mnop and i have 4 txt files, i want to add each line to each file, like file 1 gets abcd at the end; file 2 gets efgh at the end .... I tried with: cat test | while read -r... (6 Replies)
Discussion started by: azaiiez
6 Replies

2. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

3. UNIX for Dummies Questions & Answers

How to add a header to a tab delimited .txt file?

Hi, I have a tab delimited document with 18 columns. My file looks like: comp1000201_c0_seq1 comp1000201_c0 337 183.51 0.00 0.00 0.00 0.00 ---NA--- 337 0 0 - comp1000297_c0_seq1 comp1000297_c0 612 458.50 ... (1 Reply)
Discussion started by: alisrpp
1 Replies

4. Shell Programming and Scripting

create txt file form data file and add some line on it

Hi Guys, I have file A.txt File A Data AK1521 AK2536 AK3164 I want create text file of all data above and write some data on each file. want Output on below folder /home/kka/out AK1521.txt Hi Welocme (3 Replies)
Discussion started by: asavaliya
3 Replies

5. UNIX for Dummies Questions & Answers

Add a new column to txt file containing filename

I would like help adding a new column to a large txt file (~10MB) that contains the filename. I have searched other posts but have not found an adequate solution. I need this extra column so I can concatenate >100 files and perform awk searches on this large file. My current txt file look... (4 Replies)
Discussion started by: kellywilliams
4 Replies

6. Shell Programming and Scripting

how to add extra a field in a flat txt file ?

Hi all, I did not use UNIX for a long time, now i need to make a flat file with extra field, can you help me with the code ? 1. I create a last line of each log from each system and make it in a flat text file (seperate by a pipe |) mv current.log old tail -1 sanfrancisco.log > current.log... (5 Replies)
Discussion started by: britney
5 Replies

7. Shell Programming and Scripting

Find similar entry in a .txt file acting as a database.

May i know how do i go along finding similar entry in a .txt file, which is used a as a "database" and post and error saying the entry existed when we key in the entry. ---------- Post updated at 05:18 PM ---------- Previous update was at 05:16 PM ---------- i mean post an error saying the... (5 Replies)
Discussion started by: santonio
5 Replies

8. Shell Programming and Scripting

Generate a mail when you add info to a txt file

Hi, I have an application's log file: /var/log/logfile which is feeded from time to time due to an application. This file contains data, what I want is: -Whenever some new data is copied to /var/log/logfileI want to generate an email to root BUT only with the new added data in the body.... (6 Replies)
Discussion started by: iga3725
6 Replies

9. Shell Programming and Scripting

how to create file.txt and add current date in file content

Hey guy, how to make bash script to create foo.txt file and add current date into file content and that file always append. example: today the script run and add today date into content foo.txt and tomorrow the script will run and add tomorrow date in content foo.txt without remove today... (3 Replies)
Discussion started by: chenboly
3 Replies
Login or Register to Ask a Question