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

将grep多行输出转为CSV格式:补全缺失字段

Solution: Convert Field Matches to CSV Format

Use this awk script to directly process your files and output the required CSV, handling missing fields and ensuring compatibility:

awk '
function quote(s) {
    if (s ~ /[",]/) {
        s = gensub(/"/, "\"\"", "g", s)
        return "\"" s "\""
    }
    return s
}
BEGIN {
    OFS = ","
    print "FILE", "NAME", "POOL", "SCHEDULE", "STORAGE", "TYPE"
    prev_file = ""
}
{
    split($0, parts, /:/)
    curr_file = parts[1]
    split(parts[2], field_val, / = /)
    field = field_val[1]
    value = field_val[2]

    if (curr_file != prev_file) {
        if (prev_file != "") {
            print quote(prev_file), quote(name), quote(pool), quote(schedule), quote(storage), quote(type)
        }
        name = pool = schedule = storage = type = ""
        prev_file = curr_file
    }

    if (field == "Name") name = value
    else if (field == "Pool") pool = value
    else if (field == "Schedule") schedule = value
    else if (field == "Storage") storage = value
    else if (field == "Type") type = value
}
END {
    if (prev_file != "") {
        print quote(prev_file), quote(name), quote(pool), quote(schedule), quote(storage), quote(type)
    }
}' * | sort

How It Works:

  1. Header Initialization: The BEGIN block prints the required CSV header and sets up tracking variables.
  2. Line Processing:
    • Splits each line into filename, field name, and value (handling the filename:Field = Value format from grep).
    • Triggers output for the previous file when switching to a new one, then resets field values for the new file.
    • Maps field names to corresponding variables to collect data per file.
  3. CSV Compatibility: The quote function wraps values in quotes if they contain commas or double quotes, escaping internal quotes by doubling them (per CSV standards).
  4. Final Output: The END block ensures the last file's data is printed.
  5. Sorting: Pipes output to sort to maintain the ordered grouping from your original command.

Usage Notes:

  • Replace * with *.cfg if you only want to process .cfg files.
  • If your values don't contain commas or quotes, you can remove the quote function and simplify the print statements to use unquoted values.

Example Output:

FILE,NAME,POOL,SCHEDULE,STORAGE,TYPE
IRVLinuxDefault.cfg,IRVLinuxDefault,IRV_DD890_Full60,IRV_Backups,IRV_SD_DD890,Backup
LVS_60Day_NDMP_Defs.cfg,LVS_DD_AV_NDMP,,LVS_NDMP_Monthly,LVS_SD_DD990_AV_NDMP,Backup
LVS_60Day_NDMP_NOFileSet_Defs.cfg,,LVS_NDMP_Monthly,LVS_SD_DD990_AV_NDMP,Backup

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 12:20:37