Split a command into the stages separated by |, then identify its output destination. For example, cat log.txt | sort >> out.txt has two stages and appends to out.txt. Read the text only; do not run commands or write files.
Input: One line containing command text, optional | separators, and an optional trailing > filename or >> filename.
Output: Join the normalized stages with ->. Add one space, followed by none, redirect to filename, or append to filename.
Input:
echo hello | wc -c
Output:
echo hello->wc -c none
Explanation: The pipe separates echo hello and wc -c. Join those stages with -> and append none because no output destination is supplied.
Input:
cat app.log | grep ERROR | wc -l
Output:
cat app.log->grep ERROR->wc -l none
Explanation: Trim the three stages and normalize their internal spaces. Join cat app.log, grep ERROR and wc -l; there is no redirection.
Input:
echo ready > status.txt
Output:
echo ready redirect to status.txt
Explanation: Remove > status.txt from the stage text. The remaining stage is echo ready and the destination is redirect to status.txt.
Input:
cat access.log | sort >> sorted.log
Output:
cat access.log->sort append to sorted.log
Explanation: Recognize >> as append redirection, not >. The stages are cat access.log and sort, and the destination is append to sorted.log.
Input:
pwd
Output:
pwd none
Explanation: pwd is one stage with no pipe or redirection. Keep it unchanged and append none.
If you look for > first, what happens to >>? Separate the destination before splitting the stages.
Before connecting commands, a shell needs to identify which stages belong in the pipeline. Level 4 executes controlled pipeline examples; this Prequel only parses their plan.
System 1: Build a Command-Line Shell
Optional application of the idea. You write the System solution separately.
Move a fixed-size window across a list without adding the same values repeatedly.
echo hello->wc -c none