Rearranging Columns with cut: Principles, Limitations, and Alternatives

Dec 08, 2025 · Programming · 11 views · 7.8

Keywords: cut command | column rearrangement | Shell scripting

Abstract: This article delves into common issues when using the cut command to rearrange column orders in Shell environments. By analyzing the working principles of cut, it explains why cut -f2,1 fails to reorder columns and compares alternatives such as awk and combinations of paste with cut. The paper elaborates on the relationship between field selection order and output order, offering various practical command-line techniques to help readers choose tools flexibly when handling CSV or tab-separated files.

Field Selection Mechanism of the cut Command

In Shell command-line tools, the cut command is commonly used to extract specific fields from text files or standard input. Its basic syntax supports specifying field lists via the -f option, e.g., cut -f2,1 file.txt. However, many users find in practice that this command does not rearrange columns as expected.

Root Cause: Consistency Between Output Order and Reading Order

According to the cut(1) manual page: Selected input is written in the same order that it is read, and is written exactly once. This means that cut strictly adheres to the original order of input data when processing fields. When -f2,1 is specified, the command first reads field 1, then field 2, but outputs them in the reading order (i.e., field 1 before field 2), not according to the parameter list order. This design ensures processing efficiency but limits flexibility in column rearrangement.

Alternative 1: Using awk for Flexible Rearrangement

Compared to cut, awk offers more powerful field handling capabilities. By explicitly specifying output order, columns can be easily rearranged. For example:

awk '{ print $2 " " $1 }' file.txt

This command outputs the second field first, followed by the first field, separated by a space. To maintain tab separation, modify it to awk '{ print $2 "\t" $1 }'. awk's flexibility also extends to supporting conditional judgments, formatting adjustments, and other advanced operations, making it suitable for complex data processing scenarios.

Alternative 2: Combining cut and paste Commands

Another approach is to use the paste command to merge outputs from multiple cut commands. For example:

paste <(cut -f2 file.txt) <(cut -f1 file.txt)

This command extracts two fields separately via process substitution and then merges them in the specified order using paste. If the system does not support process substitution, an alternative method can be used:

paste file.txt file.txt | cut -f2,3

This method achieves rearrangement by duplicating file content and selecting new field combinations, though it is slightly redundant, it offers broader compatibility.

Comparison of Solutions and Applicable Scenarios

The awk solution is the most direct and powerful, suitable for scenarios requiring complex logic or format control. The combination of cut and paste is more advantageous for simple rearrangement or resource-constrained environments. In practice, choices should consider data scale, delimiter type, and subsequent processing needs. For instance, for large files, the efficiency of cut might be preferred; for irregular data, awk's error-handling capabilities are more critical.

Summary and Best Practices

Understanding the intrinsic mechanisms of tools is key to effectively using command-line utilities. While the cut command is simple and efficient, its immutable field output order limits column rearrangement functionality. In practical work, it is recommended to: first clarify requirements; if only simple rearrangement is needed, try awk first; if handling large-scale data while maintaining high performance, evaluate the combination of cut and paste. Regardless of the chosen solution, results should be validated through small-scale testing to ensure data integrity and format correctness.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.