Can anyone please help with this? I have 2 files as given below.
If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value.
file1
abc_1 foo1@a ....
abc_1 soo2@a ...
def_2 soo2@a ....
def_2 foo1@a .....
ghi_3 foo1@a .....
ghi_3 soo1@a
file2
def_2 foo2@a
def_2 soo2@a
abc_1 foo3@a
abc_1 soo1@a
ghi_3 foo2@a
ghi_3 soo1@a
Required output ( file1 modified to be):
abc_1 foo3@a
abc_1 soo2@a
def_2 soo2@a
def_2 foo2@a
ghi_3 foo2@a
ghi_3 soo1@a
awk 'NR==FNR { A[$1]=$2; O[++L]=$1; next }; ($1 in A) { A[$1]=$2 }; END { for(N=1; N<=L; N++) print O[N], A[O[N]]; }' file1 file2 > new_file1
Hi! Thanks for the reply.It does not produce the desired output, though.
What I end up with is duplicate column2 for the same column1, in file1.
For example, the o/p from the awk code gives:
abc_1 soo1@a
abc_1 soo1@a
def_2 soo2@a
def_2 soo2@a
ghi_3 soo1@a
ghi_3 soo1@a
$ cat repcol.awk
NR==FNR && $1 { I[$1]++; A[$1,I[$1]]=$2; next }
$1 { O[$1]++; if( ($1 SUBSEP O[$1]) in A) A[$1,O[$1]]=$2; }
END {
for(X in A)
{
split(X, B, SUBSEP)
print B[1], A[X];
}
}
$ awk -f repcol.awk data1 data2
abc_1 foo3@a
abc_1 soo1@a
def_2 foo2@a
ghi_3 foo2@a
def_2 soo2@a
ghi_3 soo1@a
$
Thank you, it works! But it re-arranges the lines in file1.I'd like to keep the same order. Also, if there are multiple columns after column2( say column 3, column 4... column 8) in file1 that needs to be retained,how would the code have to be modified?
Your code o/p is now
ghi_3 foo2@a
ghi_3 soo1@a
abc_1 foo3@a
abc_1 soo1@a
def_2 foo2@a
def_2 soo2@a
---------- Post updated at 02:06 PM ---------- Previous update was at 01:12 PM ----------
Would you by any chance be able to modify the existing code to get that done?Also, would you pls. explain what you have here?
Bumping up posts or double posting is not permitted in these forums.
Please read the rules , which you agreed to when you registered, if you have not already done so.
You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future
Thank You.
The UNIX and Linux Forums.
FYI, your bumping didn't even bring me here. I was reviewing my own posts for replies as I sometimes do.
prashali:
Thank you, it works! But it re-arranges the lines in file1.I'd like to keep the same order. Also, if there are multiple columns after column2( say column 3, column 4... column 8) in file1 that needs to be retained,how would the code have to be modified?
Your original output didn't keep the columns, so neither did I.
Working on it.
I replaced the indistinguishable dots of data after the second column with abc, def, ghi, jkl, etc.
$ cat repcol2.awk
NR==FNR && $1 {
I[$1]++
A[$1,I[$1]]=$0
O[++L]=$1 SUBSEP I[$1]
next }
$1 {
O[$1]++;
if( ($1 SUBSEP O[$1]) in A)
{
T=$2;
$0=A[$1,O[$1]];
$2=T;
A[$1,O[$1]]=$0;
}
}
END { for(N=1; N<=L; N++) print A[O[N]]; }
$ awk -f repcol2.awk data1 data2
abc_1 foo3@a abc
abc_1 soo1@a def
def_2 foo2@a ghi
def_2 soo2@a jkl
ghi_3 foo2@a mno
ghi_3 soo1@a pqr
$