Learn Practice Daily golf Reference
Real-world refactors·Lesson 5 of 6·11 min·0/6 exercisesPro

Swap and reformat CSV columns

Launch week, Friday: the export reshaped. One slip with five circled columns, tested on one row first, another for cents, and a block with $A for a new column.

Scene

Friday of launch week, 9:00 a.m. The accountant, one last time this quarter: the new system wants the order export with the status second, prices in cents instead of dollars, and a currency column at the end. Hundreds of rows; the sample here has eight.

Every row has the same shape, which is exactly what slips and blocks are for.

From memory

Warm-up from earlier chapters

Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.

Change the string's text to Guest, from anywhere inside it.
press the keys…9 keys
1 of 2
Change the string's text to Guest, from anywhere inside it.

Circle every column

To reorder columns, circle each one in the pattern and put them back in the new order: ([^,]*) is everything up to the next comma, so five of those circles the five columns, and the replacement \1,\5,\2,\3,\4 moves the fifth to second. In very magic mode (\v), plain brackets circle. For a column that's the same on every line, a block is quicker: Ctrl-v, down, $A.

circle\1\2\3\4\5
columnorder_iditem_namequantityunit_priceorder_status
put back1st3rd4th5th2nd
Before
2201,al-pastor,1,9.50,delivered
:s/\v^([^,]*),([^,]*),([^,]*),([^,]*),(.*)$/\1,\5,\2,\3,\4/↵
2201,delivered,al-pastor,1,9.50
Five circles, put back in a new order: the status moves to second place.
Check yourself

A comma in quotes

12209,"burrito, large",1,9.50,delivered
NORMAL
Keys pressed: :s/\v^([^,]*),([^,]*),([^,]*),([^,]*),(.*)$/\1,\5,\2,\3,\4/↵

Another shop's export puts quotes around a name with a comma in it. What does the same slip make of this row?

Pick an answer (or press its number), then watch the engine prove it.
Boss round

Stage 1 · The order of the columns

9:10. The last column moves to second place, on every line, header included. First test the slip on one row: the cursor is on the first one, and :s changes only this line. Check that delivered came second. Then undo the test with u and send the same slip to every line with g& (from the Ex chapters: the last slip, to the whole file).

Stuck? Show a hint:s/\v^([^,]*),([^,]*),([^,]*),([^,]*),(.*)$/\1,\5,\2,\3,\4/<CR> · then u g&
1 of 2Fix the marked spots · 2 left
1order_id,item_name,quantity,unit_price,order_status
22201,al-pastor,1,9.50,delivered
12202,birria,4,11.00,refunded
22203,veggie,3,8.25,delivered
32204,pollo,2,9.00,late
42205,carnitas,1,10.00,delivered
52206,al-pastor,4,9.50,delivered
62207,birria,3,11.00,delivered
72208,veggie,2,8.25,late
NORMALLoading the editor…1/20.0s2:1

This exercise needs a keyboard. Read on, and play it at your computer.

Boss round

Stage 2 · Cents

9:30. Prices in cents: 9.50 becomes 950 on every row (the price is at the end of each line now), and the header's unit_price becomes price_cents. The header and the rows need different changes.

Stuck? Show a hint
Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

This exercise needs a keyboard. Read on, and play it at your computer.

Boss round

Stage 3 · The currency

9:50. A last column: ,currency on the header, ,USD on every row. The rows end at different lengths.

Stuck? Show a hint
Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

This exercise needs a keyboard. Read on, and play it at your computer.

The import screenFinish the exercise above to see it.
From memory

Columns, from memory

Press the keys.

Swap the first two comma-separated columns on every line.
scroll here, then press the keys30 keys
1 of 2
Swap the first two comma-separated columns on every line.