......
ATOM 91 H2'' G A 3 17.357 8.753 -30.401 1.00 0.00 A
ATOM 92 O2' G A 3 16.590 9.059 -28.495 1.00 0.00 A
ATOM 93 H2' G A 3 16.670 9.792 -27.880 1.00 0.00 A
ATOM 94 O3' G A 3 16.875 11.895 -29.146 1.00 0.00 A
ATOM 95 P U A 4 17.646 13.251 -28.975 1.00 0.00 A
ATOM 96 O1P U A 4 18.619 13.509 -30.118 1.00 0.00 A
ATOM 97 O2P U A 4 18.188 13.245 -27.547 1.00 0.00 A
.......
File #2:
Code:
......
H3' T 1.32
C2' T 2.01
H2'' T 1.34
H2' T 1.34
O3' T 1.77
P G 2.15
O1P G 1.70
O2P G 1.70
O5' G 1.77
H2'' G 1.34
......
For File#1, I want to add a column. The content of this column is from File#2.
The procedure is, for each line in File#1,
first, search the line in File#2 satisfies: $1(in File#2) == $3(in File#1), $2(in File#2) == $4(in File#1),
e.g., for line#1 in File#1, find the line in File#2 satisfy $1==H2'' and $2==G ;
then, add $3 of File#2 to the end of the line in File#1, e.g., add 1.34 to the end of line#1 in File#1.
Thank you!
Last edited by rockytodd; 08-14-2009 at 03:38 PM..
To keep the forums high quality for all users, please take the time to format your posts correctly.
Use Code Tags when you post any code or data samples so others can easily read your code.
You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)
Avoid adding color or different fonts and font size to your posts.
Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.
From your example, that script can do what you want:
Code:
#!/bin/sh
cat /dev/null > file3
while read line1; do
c1=$(echo ${line1} | cut -d' ' -f3-4)
while read line2; do
c2=$(echo ${line2} | cut -d' ' -f1-2)
if [ "${c1}" = "${c2}" ]; then
echo "${line1} $(echo ${line2} | cut -d' ' -f3)" >> file3
fi
done < file2
done < file1
exit 0
the first command, we create an empty file3 ;
the first while loop: we read a ${line1} from file1 and we extract the third and fourth fields;
the second while loop, we read ${line2} from file2, we extract the first two fields, then we compare the two strings. If they match, we add ${line1} and the third field from file2 on file3.
Awesome, but I can't understand either ...
From what I can read, a[$1FS$2] seems to be related to file2 (FS is a field separator)
NR and FNR are built-in awk variables
a[] is an array, but why is it refering to file2... mystery ^^ or maybe [$1FS$2] is a character class, I don't understand , still reading the man for more infos
awk '
NR==FNR{ # process first file(file2 as reference file)
a[$1FS$2]=$3 # store file2 field $3 information in array a indexed by key $1FS$2 where FS is the original field separator
next # process next line
}
{ # process second file (file2)
print $0,a[$3FS$4] # print each line and the array a element where the key is $3FS$4 from second file(3rd and 4th field divided by FS)
}
' file2 file1
Hi All,
I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record.
For example, let's assume for column D... (9 Replies)
Hi,
My input files is like this
axis1 0 1 10
axis2 0 1 5
axis1 1 2 -4
axis2 2 3 -3
axis1 3 4 5
axis2 3 4 -1
axis1 4 5 -6
axis2 4 5 1
Now, these are my following tasks
1. Print a first column for every two rows that has the same value followed by a string.
2. Match on the... (3 Replies)
Hello experts,
Please help me in achieving this in an easier way possible. I have 2 csv files with following data:
File1
08/23/2012 12:35:47,JOB_5330
08/23/2012 12:35:47,JOB_5330
08/23/2012 12:36:09,JOB_5340
08/23/2012 12:36:14,JOB_5340
08/23/2012 12:36:22,JOB_5350
08/23/2012... (5 Replies)
I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that?
File 1:
100 rs3794811 0.01 0.3434
100 rs8066551 0.01... (8 Replies)
Hi guys,
I'm rather new at using UNIX based systems, and when it comes to scripting etc I'm even newer.
I have two files which i need to compare.
file1: (some random ID's)
451245
451288
136588
784522
file2: (random ID's + e-mail assigned to ID)
123888 xc@xc.com
451245 ... (21 Replies)
Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2
file 1 sample
SNDK 80004C101 AT
XLNX 983919101 BB
NETL 64118B100 BS
AMD 007903107 CC
KLAC 482480100 DC
TER 880770102 KATS
ATHR 04743P108 KATS... (7 Replies)
ex:
a file has :
122323
123456456
125656879
678989965t635
234323432
b has :
this is finance no.
this is phone no
this is extn
ajkdgag
idjsidj
i want the o/p as:
122323 his is finance no.
123456456 this is phone no
123456456 ... (4 Replies)