A comprehensive solution and detailed explanation for solving LeetCode Problem 194: Transpose File. This guide includes the problem statement, approach, and code implementation.
cat file.txt | awk '{ for (i = 1; i <= NF; i++) { if (NR == 1) { cols[i] = $i } else { cols[i] = cols[i] " " $i } } } END { for (i = 1; i <= length(cols); i++) { print cols[i] } }'
Transposing a File with
awk
Transposing a file means converting rows into columns and columns
into rows. This is useful in various data manipulation tasks.
awk Basics
NF (Number of Fields): The number of
fields in the current line (record). (column)
NR (Number of Records): The line
number or the number of records read so far. Incremented automatically
with each new line. (row)
awk Script
for Transposing Explanation
1 2 3 4 5 6 7 8 9
{ for (i = 1; i <= NF; i++) { if (NR == 1) { cols[i] = $i } else { cols[i] = cols[i] " "$i } } }
The {} block is executed for each record (line) read
from the input.
Loop Through Fields
1
for (i = 1; i <= NF; i++) {
NF (Number of Fields): Number of fields in the current
record.
Loops through each field from 1 to
NF.
First Record Handling
1 2 3
if (NR == 1) { cols[i] = $i }
NR (Number of Records): The current record number.
For the first record, initialise the cols array with
fields from the first line.
Subsequent Records
1 2 3
else { cols[i] = cols[i] " "$i }
For subsequent lines, append the current field $i to
the existing string in cols[i], separated by a space.
Handling the End of Input
1 2 3 4 5
END { for (i = 1; i <= length(cols); i++) { print cols[i] } }
The END block executes after all lines have been
processed.
Prints each column (now a row in the transposed format).
Example Workflow
Given file.txt:
1 2 3
name age alice21 ryan30
Processing Each Record
First Record (NR == 1)
NF is 2 (two fields: name and
age).
The loop runs from i = 1 to i = 2.
1 2
cols[1] = "name" cols[2] = "age"
Second Record (NR == 2)
NF is 2 (two fields: alice and
21).
The loop runs from i = 1 to i = 2.
1 2
cols[1] = "name alice" cols[2] = "age 21"
Third Record (NR == 3)
NF is 2 (two fields: ryan and
30).
The loop runs from i = 1 to i = 2.
1 2
cols[1] = "name alice ryan" cols[2] = "age 21 30"
Final Output
1 2
name alice ryan age 21 30
Debugging the awk
Script
You can add print statements inside your
awk script to debug and see what's happening at each
step.
Debugging Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
cat file.txt | awk '{ print "Processing line:", NR for (i = 1; i <= NF; i++) { if (NR == 1) { cols[i] = $i } else { cols[i] = cols[i] " " $i } print "cols[" i "] =", cols[i] } } END { print "\n[RESULT]" for (i = 1; i <= length(cols); i++) { print cols[i] } }'
Output with Debugging
Statements
1 2 3 4 5 6 7 8 9 10 11 12 13
Processing line: 1 cols[1] = name cols[2] = age Processing line: 2 cols[1] = name alice cols[2] = age 21 Processing line: 3 cols[1] = name alice ryan cols[2] = age 21 30