将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:
- Header Initialization: The
BEGINblock prints the required CSV header and sets up tracking variables. - Line Processing:
- Splits each line into filename, field name, and value (handling the
filename:Field = Valueformat 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.
- Splits each line into filename, field name, and value (handling the
- CSV Compatibility: The
quotefunction wraps values in quotes if they contain commas or double quotes, escaping internal quotes by doubling them (per CSV standards). - Final Output: The
ENDblock ensures the last file's data is printed. - Sorting: Pipes output to
sortto maintain the ordered grouping from your original command.
Usage Notes:
- Replace
*with*.cfgif you only want to process.cfgfiles. - If your values don't contain commas or quotes, you can remove the
quotefunction and simplify theprintstatements 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
相关产品推荐
相关产品推荐

