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

tcsh环境下迁移含相同指定位置子串的文件名列表条目实现方法

tcsh 实现方案

首先提前配置3个可自定义参数:

  • 所有待处理的列表文件集合:set lists = (file1 file2 file3) 替换为你实际的文件路径
  • 子串截取范围:set substr_range = "4-7" 示例中0基3-6位对应cut的1基4-7位,你原有逻辑的10-14直接替换即可
  • 迁移目标文件:set target_file = "file3" 替换为你要统一迁入的文件路径

完整代码

# 配置项
set lists = (file1 file2 file3)
set substr_range = "4-7"
set target_file = "file3"

# 1. 生成所有条目对应【子串|所属文件|原内容】的临时映射
rm -f /tmp/all_entries.tmp
foreach list ($lists)
  foreach line (`cat "$list"`)
    set substr = `basename "$line" | cut -c $substr_range`
    echo "$substr|$list|$line" >> /tmp/all_entries.tmp
  end
end

# 2. 提取所有出现次数≥2的公共子串(需要迁移的子串)
set duplicate_substrs = (`awk -F '|' '{print $1}' /tmp/all_entries.tmp | sort | uniq -d | tr '\n' ' '`)

# 3. 逐个处理列表文件,拆分保留内容和待迁移内容
foreach list ($lists)
  if ("$list" == "$target_file") continue
  rm -f /tmp/keep.tmp /tmp/move.tmp
  touch /tmp/keep.tmp /tmp/move.tmp
  foreach line (`cat "$list"`)
    set substr = `basename "$line" | cut -c $substr_range`
    set is_dup = 0
    foreach s ($duplicate_substrs)
      if ("$substr" == "$s") then
        set is_dup = 1
        break
      endif
    end
    if ($is_dup) then
      echo "$line" >> /tmp/move.tmp
    else
      echo "$line" >> /tmp/keep.tmp
    endif
  end
  # 保留内容覆盖回原文件
  mv /tmp/keep.tmp "$list"
  # 待迁移内容追加到目标文件
  cat /tmp/move.tmp >> "$target_file"
end

# 清理临时文件
rm -f /tmp/all_entries.tmp /tmp/keep.tmp /tmp/move.tmp

注意事项

如果文件名包含空格,foreach line (\cat "$list"`)会按空格拆分条目,这种情况可以改用while`逐行读取的写法适配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 20:45:03