基于Bash脚本修复合并的|分隔43列数据:恢复换行符
Fixing the Line Splitting Script for 43-Column Data
Your original approach has a couple of key issues—invoking sed from within awk like that won’t work as intended, and the logic doesn’t properly group fields into valid 43-column chunks. Let’s rewrite this entirely in awk (no external commands needed) to efficiently split lines with excess columns into the correct format:
awk -F'|' '{ # Iterate through fields in groups of 43 for (start=1; start<=NF; start+=43) { # Build the output line by joining the next 43 fields output = $start for (offset=1; offset<43 && (start+offset)<=NF; offset++) { output = output "|" $(start+offset) } # Print the completed 43-column line print output } }' your_input_file.txt
How this works:
- We use
awkwith|as the field separator to split each line into individual columns. - For each line, we loop through the fields in steps of 43. For each starting position:
- We initialize the output line with the first field of the current group.
- We append the next 42 fields (completing 43 total) using
|as the separator. - We print the finished line, which will have exactly 43 columns (if your original merged data is a perfect multiple of 43 columns, which it should be per your problem description).
This script is far more efficient than your original attempt because it handles all processing internally in awk instead of spawning external sed processes, and it guarantees every output line meets the 43-column requirement.
内容的提问来源于stack exchange,提问作者vinay kumar
相关产品推荐
相关产品推荐

