You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:编写Korn Shell脚本递归查找竖线分隔文件的最高管理者

Got it, let's tackle this problem step by step. You need a Korn Shell (ksh) script that recursively finds the top-level manager (ultimate boss) for each employee in a pipe-delimited file. I'll walk you through a robust solution, explain how it works, and include test examples to verify it.


Assumed Input/Output Structure

First, let's align on the typical structure I'll build this for (adjust if your format differs):

Input File Format: Each line follows employee_id|employee_name|manager_id

  • Top-level managers will have an empty manager_id, NULL, or their own ID as the manager value.
    Expected Output: Each line includes the employee's details + their ultimate manager's ID and name:
    employee_id|employee_name|top_manager_id|top_manager_name

Korn Shell Script Solution

#!/usr/bin/ksh

# Replace with your actual input file path
INPUT_FILE="employees.txt"

# Associative arrays to store employee data for fast lookups
typeset -A emp_name
typeset -A emp_manager

# Load all employee records into the arrays first
while IFS='|' read -r emp_id emp_name_val manager_id; do
    emp_name["$emp_id"]="$emp_name_val"
    emp_manager["$emp_id"]="$manager_id"
done < "$INPUT_FILE"

# Recursive function to trace up to the top-level manager
get_top_manager() {
    local current_emp_id="$1"
    local current_manager_id="${emp_manager[$current_emp_id]}"

    # Check if we've reached the top-level manager
    if [[ -z "$current_manager_id" || "$current_manager_id" == "$current_emp_id" || "$current_manager_id" == "NULL" ]]; then
        echo "$current_emp_id|${emp_name[$current_emp_id]}"
        return
    fi

    # Recurse up to the current manager's boss
    get_top_manager "$current_manager_id"
}

# Generate the output
echo "employee_id|employee_name|top_manager_id|top_manager_name"
while IFS='|' read -r emp_id emp_name_val _; do
    top_manager_details=$(get_top_manager "$emp_id")
    echo "$emp_id|$emp_name_val|$top_manager_details"
done < "$INPUT_FILE"

How This Works

Let's break down the key parts:

  1. Data Loading: We first read the entire input file into two associative arrays. This lets us look up any employee's name or manager in constant time, which makes the recursive checks efficient.
  2. Recursive Lookup: The get_top_manager function takes an employee ID, checks if they're the top-level manager (no manager, self-managed, or marked as NULL). If not, it calls itself with the current employee's manager ID, continuing up the chain until it hits the top.
  3. Output Generation: We loop through each employee in the input, run the recursive lookup, and format the results into the desired pipe-delimited output.

Test Example

Input File (employees.txt)

101|Alice|103
102|Bob|101
103|Charlie|NULL
104|Diana|102
105|Eve|103

Running the Script

chmod +x find_top_manager.ksh
./find_top_manager.ksh

Output

employee_id|employee_name|top_manager_id|top_manager_name
101|Alice|103|Charlie
102|Bob|103|Charlie
103|Charlie|103|Charlie
104|Diana|103|Charlie
105|Eve|103|Charlie

Notes for Adjustments

  • Circular References: If your data has cycles (e.g., Employee A reports to B, B reports to A), the script will loop infinitely. We can add a cycle-tracking variable to handle this—just let me know if you need that!
  • Older Ksh Versions: Associative arrays require ksh93 or newer. If you're on an older version, we can adjust to use indexed arrays or file-based lookups instead.
  • Custom Formats: If your input has extra columns or uses a different placeholder for top managers, tweak the read commands and the condition checks in the recursive function.

内容的提问来源于stack exchange,提问作者Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:36:42