Skip to content

Instantly share code, notes, and snippets.

@stephenmm
Created April 30, 2021 16:03
Show Gist options
  • Save stephenmm/dc2a41c02522c54b713d2e5f037c66ee to your computer and use it in GitHub Desktop.
Save stephenmm/dc2a41c02522c54b713d2e5f037c66ee to your computer and use it in GitHub Desktop.
# Create 6 column CSV with with added zeros (Solutions w/ perl awk sed) https://stackoverflow.com/a/38106938/120681
# I have a file.txt containing numbers like
1, 2, 3
4, 5
6, 7, 8, 9,10,11
12,13,14,15,16
# I want to create a CSV file by padding each line such that there are 6 values separated by 5 commas, so I need to add to each line an appropriate number of ",0". It shall look like
1, 2, 3, 0, 0, 0
4, 5, 0, 0, 0, 0
6, 7, 8, 9,10,11
12,13,14,15,16, 0
read -r -d '' SIX_COLUMN_CSV << 'END_SIX_COLUMN_CSV'
1, 2, 3
4, 5
6, 7, 8, 9,10,11
12,13,14,15,16
END_SIX_COLUMN_CSV
echo -e "$SIX_COLUMN_CSV" > six_column.csv
# Solutions:
perl -lpe '$_ .= ",0" x (5 - tr/,//)' six_column.csv
awk -v FS=, -v OFS=, '{ for(i = NF+1; i <= 6; i++) $i = 0 } 1' six_column.csv
sed ':b /^\([^,]*,\)\{5\}/ b; { s/$/,0/; b b }' six_column.csv
read -r -d '' SIX_COLUMN_PY << 'END_SIX_COLUMN_PY'
import sys
max_width = 0
rows = []
for line in sys.stdin:
line = line.strip()
existing_vals = line.split(",")
rows.append(existing_vals)
max_width = max(max_width, len(existing_vals))
for row in rows:
zeros_needed = max_width - len(row)
full_values = row + ["0"] * zeros_needed
print ",".join(full_values)
END_SIX_COLUMN_PY
echo -e "$SIX_COLUMN_PY" > six_column.py
cat six_column.csv | python six_column.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment