Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join Files without separator

Status
Not open for further replies.

Felipin85

Programmer
Sep 12, 2014
3
ES
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.

 


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
 
Thanks for your help, LKBrwnDBA.
Can you explain me the code, please.

 

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top