Introduction
Hello, this is Turner, who has just started learning commands. This time, under the theme of "splitting with sed," I will explain how to split a URL by inserting a newline at every slash (/). Although the command itself is simple, there are many points where beginners tend to get stuck, so I will explain it carefully, including the state before and after execution as well as common failure examples.
Command Used This Time
This command splits the string by replacing each "/" in the URL with a newline. It is a basic example that lets you experience "splitting with sed."
State Before Execution (File name: input.txt)
First, let's check the content to be processed. This time, we assume a single-line URL like the following:
https://running-terminal-commands/sed-split-url
In this state, the entire URL is on one line, making the structure a bit hard to read.
State After Execution (File name: output.txt)
When you run the command, the slashes are replaced with newlines as shown below:
https:
running-terminal-commands
sed-split-url
This makes the structure of the URL visually much easier to understand. It is especially useful for log analysis and checking path structures.
Execution image

Understanding How the Command Works
Since this is a part where beginners often stumble, I will break it down and explain it step by step.
- echo "string" → Outputs the specified string.
- |(Pipe) → Passes the output from the left side to the command on the right side.
- sed 's/\//\n/g'→ Performs a substitution operation.
- s:Substitution command
- /\//:Specifies a slash (escaping is required)
- /\n/:Replaces it with a newline
- g:Applies the replacement to all matches (global)
The key point here is that since "/" is used as the delimiter in sed, it must be escaped with a backslash (\).
Common Failure Examples (3 Cases)
① Forgetting to escape the slash
sed 's///\n/g'
→ This causes an error. Because "/" is the delimiter, writing it without escaping breaks the syntax.
② Forgetting the single quotes
sed s/\//\n/g
→ The shell will interpret the command, so it won't work as intended. Always enclose the sed expression in single quotes ' '.
③ Forgetting to specify the input file
sed 's/\//\n/g' > output.txt
→ Nothing is output, or the command waits for input. This happens because "no processing target file is passed to sed." As a result, it enters a waiting state for standard input.
The correct way is to specify the target file:
Benefits of Using sed for Splitting
"Splitting with sed" is useful in the following situations:
- When you want to check the structure of a URL or path
- When you want to make a log file easier to read
- When you want to split text by a specific delimiter
It is especially important as a fundamental text-processing technique when writing shell scripts.
Summary
This time, we learned how to split a string by newlines using sed.
- Understand the basic form s/pattern/replacement/g
- Pay attention to escaping slashes
- Understand that one reason a command appears to "stop" is because it is waiting for input
Although the command is simple, once you understand it, the range of applications expands greatly. Even I stumbled at first over "why the backslash is needed," but once I understood the mechanism, it made perfect sense.
Let’s continue getting used to commands little by little!
