How to append two files(file1 and file2) Please help me?

Hi

I have two files file1 and file2
File1 10,000 entries:It has 3 columns below.

conn=232257   client=xxx.xxx.xx.xxx:60491  protocol=LDAP

File2 has 500 entries It has two columns.

conn=232257   dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxxx,dc=xxxx"
conn=232398 dn="uid=yyyy,ou=yyyyy,ou=yyyyy,dc=yyyy,dc=yyy"

Output file should be like below with four columns: If first column matches between file1 and file2 then shell command should add column-2 from file-2 to file-1 as column-4. Sorry for confusion. Output should be like this.

O/p:

conn=232257   client=xxx.xxx.xx.xxx:60491  protocol=LDAP  dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxxx,dc=xxxx"

This should be easily done with awk.
With 31 posts already, how far have you gotten in your implementation AND where exactly are you stuck?
Please show the effort!

Hi

I'm newe to shell and this is my first post can you please share the solution.
I tried using awk as below but i did't understand few parameters so i posted here for solution.

awk 'NR==FNR {h[$2] = $3; next} {print $1,$2,$3,h[$2]}' file1 file3
awk 'NR==FNR {h[$1] = $2; next} {print $0,h[$1]}' file2 file1

btw, this is NOT your first post - you have 31 posts already!

Hi Vgersh

Sorry i did't get my expected results after running your command which you posted. Can you please look in to my requirement which i posted with example.

I need 4 coulms in my output file like below.

conn=232257  client=xxx.xxx.xxx.xx:51237 protocol=LDAP    dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxx,dc=xxx"

given file1:

conn=232257   client=xxx.xxx.xx.xxx:60491  protocol=LDAP

and file2:

conn=232257   dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxxx,dc=xxxx"
conn=232398 dn="uid=yyyy,ou=yyyyy,ou=yyyyy,dc=yyyy,dc=yyy"

running the following:

awk 'NR==FNR {h[$1] = $2; next} {print $0,h[$1]}' file2 file1

produces the following output:

conn=232257   client=xxx.xxx.xx.xxx:60491  protocol=LDAP dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxxx,dc=xxxx"

What's wrong with this output?
What do you get? And how's that different from the output above?
Please be as specific as possible AND use code tags when posting your code and/or data samples.

Hi

I know its 31 posts but i'm not in to this filed.
After running your command,i see ourput is merged two files(file1 and file2) into file3

---------- Post updated at 03:29 PM ---------- Previous update was at 03:09 PM ----------

Hi
Can you explain what is h[$1],$1,$2,$0 values so that i can change code accordingly

awk 'NR==FNR {h[$1] = $2; next} {print $0,h[$1]}' file2 file1

Please modify your latest post using code tags.

Hi

i have file1:

conn=232257   client=16218.19488.218.86:51237     protocol=LDAP

file2:

conn=232257      dn="uid=apple,ou=xxxx,ou=usfgfhfers,dc=example,dc=com"
conn=232370       dn="uid=ball,ou=yyyyyy,ou=usfhfhfhers,dc=example,dc=com"

In the output file it should match first column from above both files then only it should add as below.
Required output should be with 4 columns

conn=232257   client=16218.19488.218.86:51237     protocol=LDAP  dn="uid=apple,ou=xxxx,ou=usfgfhfers,dc=example,dc=com

But command which you provided priniting all ines from both files which is wrong

---------- Post updated at 04:23 PM ---------- Previous update was at 03:55 PM ----------

This is the code. Dont post if you have not validate the solution which you providing. Please learns basics

/usr/xpg4/bin/awk 'NR==FNR {a[$1]=$2; next} $1 in a {print $0, a[$1]}' file4 file3

I'm getting exactly the output that you wanted.
Please provide the exact command that you're running (word by word - not what's been posted here, but rather what you typed on your end).
Also please provide the output of running cat -vet file1 and cat -vet file2 .
All the asks should be provided using code tags. The absence of the code tags will render this thread closed.
Thanks.

1 Like

I won't let my peers date their time any longer, thread closed...

1 Like