Technical Analysis of Splitting Command Output by Columns Using Bash

Nov 27, 2025 · Programming · 11 views · 7.8

Keywords: Bash | Command Output Processing | Field Splitting

Abstract: This paper provides an in-depth examination of column-based splitting techniques for command output processing in Bash environments. Addressing the challenge of field extraction from aligned outputs like ps command, it details the tr and cut combination solution through squeeze operations to handle repeated separators. The article compares alternative approaches like awk and demonstrates universal strategies for variable format outputs with practical case studies, offering valuable guidance for command-line data processing.

Problem Background and Challenges

In Bash scripting, processing command output and extracting specific fields by column is a common requirement. Taking process information query as an example, executing the ps command produces output in the following format:

  PID TTY          TIME CMD
11383 pts/1    00:00:00 bash
11771 pts/1    00:00:00 ps

When attempting to extract the command name of a specific process, directly using cut -d" " -f 4 encounters field extraction failures. The core issue stems from the ps command inserting additional spaces between the second and third columns to maintain table alignment, causing actual field position shifts.

Core Solution: tr and cut Combination

To address field extraction challenges caused by repeated separators, the most effective solution combines the tr command's squeeze functionality. The specific implementation is as follows:

ps | grep 11383 | tr -s ' ' | cut -d ' ' -f 4

This approach uses tr -s ' ' to compress consecutive multiple spaces into single spaces, normalizing field separation and ensuring cut can accurately identify target field positions. This method offers the following advantages:

In-depth Technical Principle Analysis

The tr -s ' ' command performs character compression operations, working by scanning the input stream and replacing consecutive occurrences of specified characters (spaces in this case) with single instances. Taking the example output 11383 pts/1 00:00:00 bash, the processing flow is as follows:

  1. Original field separation: PID|TTY|TIME|CMD
  2. Actual space distribution: single space|single space|multiple spaces|single space
  3. After tr -s ' ' processing: All inter-field separators become single spaces
  4. cut -d ' ' -f 4 can now accurately extract the fourth field "bash"

Alternative Approach Comparison

Besides the tr-cut combination, awk provides another field processing method:

ps | grep 11383 | awk '{print $4}'

awk treats consecutive whitespace characters (spaces, tabs, etc.) as field separators by default, automatically handling excess space issues. However, compared to the tr-cut solution:

Extended Application Scenarios

This technical combination can be widely applied to various command-line output processing scenarios:

Combined with column formatting requirements mentioned in the reference article, this technology provides basic field extraction capabilities for multi-column data reorganization, enabling more complex data display formats.

Best Practice Recommendations

When processing command outputs of unknown formats, the following strategies are recommended:

  1. First use tr -s ' ' to normalize separators
  2. Perform field extraction via cut or awk
  3. For complex formats, combine with sed for preprocessing
  4. Always test edge cases to ensure solution robustness

By mastering these core technologies, various challenges in command output processing within Bash environments can be effectively resolved, enhancing command-line data processing efficiency and accuracy.

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.