×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Join Files without separator

Join Files without separator

Join Files without separator

(OP)
Hi, I'm trying a join of two files but, the format is special. For example:

file1:

11223344
12234455
22454488
55448877

file2:

11882244
13445588
55446677

Output:
11223344
55446677

The key are the positions (1,2,7,8) (numbers of columns) and these records are contains in the file1.

Thanks and sorry for my English.

RE: Join Files without separator



Try this:

CODE

awk '{k=substr($1,1,2)substr($1,7,2);o[k]=o[k]" "$1;}END{for(k in o){n=split(o[k],e);if(n>1)print e[1];}}' file1 file2 
3eyes

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

RE: Join Files without separator

(OP)
Thanks for your help, LKBrwnDBA.
Can you explain me the code, please.

RE: Join Files without separator


CODE

k=substr($1,1,2)substr($1,7,2)		- Extract "key": first and last two characters
o[k]=o[k]" "$1				- Collect each corresponding element in an array
END{for(k in o){			- At the end, loop through the array
n=split(o[k],e);if(n>1)print e[1];}	- .. print only those elements for array which has more than 1
}' file1 file2 				- source both files. 
noevil

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

RE: Join Files without separator

(OP)
Thanks, LKBrwnDBA for your help!;)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close