You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求解析BASH环境中SED命令`sed -e 's/_.*$//'`的具体含义

Breaking Down the sed -e 's/_.*$//' Command

Hey there! Since you're new to bash, let's unpack this sed command step by step so it makes total sense.

First, let's recap what the full command does in your example: when you run tnum=$(echo $1 | sed -e 's/_.*$//') with the input string HYH19986_T_DRIVER_BAG_PRESSURE__78ms_546ms, it strips everything starting from the first underscore onward, leaving you with HYH19986. Now let's dig into the part you asked about—from -e onward:

1. The -e Flag

The -e stands for "expression"—it tells sed that the text immediately following is the editing command we want it to run. You can use multiple -e flags if you need to run several commands in sequence, but here we just have one straightforward command.

2. The Core Substitution Command: s/_.*$//

This is a sed substitute command, which follows the basic structure: s/pattern/replacement/. Let's break down each component:

  • s/: Short for "substitute"—this is the signal that we want to find a pattern and replace it with something else.
  • _.*$: This is the search pattern (a regular expression):
    • _: Matches a literal underscore character (the first one in your input string).
    • .*: The dot (.) matches any single character, and the asterisk (*) means "match zero or more of the preceding character". Together, .* grabs every character that comes after the underscore.
    • $: Anchors the pattern to the end of the line. This ensures we don't stop halfway—we match everything from the first underscore all the way to the end of the input string.
  • //: This is the replacement part—since there's nothing between the slashes, we're telling sed to replace the matched pattern with... nothing. In other words, we're deleting everything that matched _.*$.

How It Works With Your Example

For your input string HYH19986_T_DRIVER_BAG_PRESSURE__78ms_546ms:

  1. sed finds the first underscore, then captures everything after it (_T_DRIVER_BAG_PRESSURE__78ms_546ms).
  2. It replaces that entire matched chunk with an empty string.
  3. What's left is just the part before the first underscore: HYH19986.

That's all there is to it! It's a simple but powerful way to strip trailing parts of a string starting at a specific character.

内容的提问来源于stack exchange,提问作者Darren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 10:37:28