employees.txt (sorted):
salaries.txt (sorted):
Understanding Basic Join:
- Default behavior: Inner join - only matching records output
- First field: Join field is first column by default (101, 102, etc.)
- Output format: Join field, followed by remaining fields from file1, then file2
- 103 and 105: Not in salaries.txt, so not in output
- 106: Not in employees.txt, so not in output
- Whitespace: Default field separator is space or tab
- Single join field: Each matching key appears once with combined data
- Sorted requirement: Both files must be pre-sorted on join field
users.csv (sorted):
purchases.csv (sorted):
Custom Delimiter Details:
- -t option: Specify any single character as field separator
- CSV files: Use -t, for comma-separated values
- /etc/passwd: Use -t: for colon-separated data
- Multiple matches: User 1002 has two purchases, appears twice in output
- Consistent delimiter: Same delimiter used for input and output
- Output structure: Join field, file1 remaining fields, file2 remaining fields
- User 1004: No purchases, doesn't appear (inner join)
- User 1005: Not in users file, doesn't appear
products.txt (sorted by field 2 - product code):
inventory.txt (sorted by field 1 - product code):
Different Field Positions:
- -1 2: Join on field 2 of first file (product code in products.txt)
- -2 1: Join on field 1 of second file (product code in inventory.txt)
- Field numbering: Fields numbered starting from 1 (not 0)
- Sort requirement: products.txt sorted on field 2, inventory.txt on field 1
- Output format: Join field first, then other fields from each file
- Keyboard (A400): Not in inventory, doesn't appear
- A500: Not in products, doesn't appear
- Flexibility: Join fields don't need to be in same position
students.txt (sorted):
grades.txt (sorted):
Left Outer Join Behavior:
- -a 1: Include all records from file1 (students.txt)
- Matched records: S101, S102, S104 appear with grade data
- Unmatched record: S103 (Carol) appears without grade - no match in file2
- Missing fields: When no match, file2 fields are simply omitted (not NULL or blank)
- All file1 data: Every student appears regardless of grade existence
- Database analogy: SQL LEFT OUTER JOIN
- Use case: "Show all students, with grades if available"
- -a 2: Would do right outer join (all records from file2)
dept_employees.txt (sorted):
dept_budgets.txt (sorted):
Full Outer Join Mechanics:
- -a 1 -a 2: Include unmatched records from both files
- D01 and D03: Matched - show employee and budget
- D02: Only in file1 - show department and employee, no budget
- D04: Only in file2 - show department and budget, no employee
- Complete picture: See all departments regardless of match status
- Data quality: Helps identify missing data in either file
- Database analogy: SQL FULL OUTER JOIN
- Use case: "Show all departments with whatever data exists"
locations.txt (sorted case-insensitively):
offices.txt (sorted case-insensitively):
Case-Insensitive Matching:
- -i flag: Ignores case when comparing join fields
- Boston vs boston: Match despite different case
- Chicago vs CHICAGO: Match despite different case
- NEW_YORK vs new_york: Match despite different case
- seattle: No match (Los_Angeles not in locations.txt)
- Output preserves case: Shows original case from file1
- Sort requirement: Files must be sorted case-insensitively: sort -f
- Real-world data: User input often has inconsistent capitalization
customers.txt (sorted):
orders.txt (sorted):
Custom Output Format:
- -o option: Specify exact output fields
- 1.2: Field 2 from file 1 (name)
- 1.3: Field 3 from file 1 (phone)
- 2.2: Field 2 from file 2 (order amount)
- -e 'NO_ORDER': Replace empty fields with "NO_ORDER"
- Bob's record: No order exists, shows NO_ORDER instead of empty field
- Field notation: FILE.FIELD where file is 1 or 2, field is column number
- Join field omitted: Can exclude join field from output if desired
- Flexible ordering: Output fields in any order: -o 2.2,1.2,1.1
registered_users.txt (sorted):
active_sessions.txt (sorted):
Unpaired Lines Analysis:
- -v 1: Show only lines from file1 with no match in file2
- -v 2: Show only lines from file2 with no match in file1
- First command output: Users registered but not currently active (U003, U004, U005)
- Second command output: Active session with no registered user (U006 - data error!)
- Data validation: Identify orphaned records or missing relationships
- Exclusive output: No matched records appear with -v
- Debugging tool: Find inconsistencies between related datasets
- Use case: Find users who haven't logged in, or sessions without users
unsorted.txt (NOT sorted):
sorted.txt (properly sorted):
Sort Verification:
- --check-order: Validates input files are sorted (default behavior in newer versions)
- --nocheck-order: Disables sort checking (not recommended)
- Error detection: Stops with error if unsorted data detected
- Line number reported: Shows where sort order violation occurs
- Prevention: Catches common mistake before producing bad output
- Silent corruption: Without check, unsorted files produce incomplete/wrong results
- Best practice: Always sort before join, let check-order verify
- Performance: Minimal overhead for sort checking
access_log.txt (extracted and sorted by user ID):
users.txt (sorted by user ID):
Practical Log Analysis:
- Data enrichment: Add user email and tier to log entries
- -a 1: Include all log entries, even if user not found
- -e 'GUEST': Mark unregistered users as GUEST
- -o custom format: Reorder: UserID, Email, Tier, Endpoint, Status, Time
- User 1005: Not in database, marked as GUEST
- User 1004: No log entries, doesn't appear (left join from logs)
- Pipeline integration: Join output fed to awk for analysis
- Real-world value: Answer questions like "How do Premium users perform?" or "Who are the guests?"