bash 使用 tr 删除特定字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10282540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:58:33  来源:igfitidea点击:

Delete a specific string with tr

bashunixcommandtr

提问by intelinside

Is it possible to delete a specific string with trcommand in a UNIX-Shell? For example: If I type:

是否可以tr在 UNIX-Shell 中使用命令删除特定字符串?例如:如果我输入:

tr -d "1."

and the input is 1.1231, it would show 23as an output, but I want it to show 1231(notice only the first 1has gone). How would I do that?

并且输入是1.1231,它将显示23为输出,但我希望它显示1231(注意只有第一个1已经消失)。我该怎么做?

If you know a solution or a better way, please explain the syntax since I don't want to just copy&paste but also to learn.

如果您知道解决方案或更好的方法,请解释语法,因为我不仅要复制粘贴还要学习。

I have huge problems with awk, so if you use this, please explain it even more.

我对 awk 有很大的问题,所以如果你使用它,请解释更多。

采纳答案by Kevin Sj?berg

In your example above the cutcommand would suffice.

在上面的示例中,cut命令就足够了。

Example: echo '1.1231' | cut -d '.' -f 2would return 1231.

示例:echo '1.1231' | cut -d '.' -f 2将返回1231.

For more information on cut, just type man cut.

有关cut 的更多信息,只需键入man cut

回答by tajhamille

You would be better off using some kind of regex (maybe something like sed).

您最好使用某种正则表达式(可能类似于 sed)。

For example, with the input 1.1231 you could use the following to get the 1231 output:

例如,对于输入 1.1231,您可以使用以下命令获得 1231 输出:

sed 's/1\.//g'

Maybe have a look here: http://tldp.org/LDP/abs/html/string-manipulation.html

也许看看这里:http: //tldp.org/LDP/abs/html/string-manipulation.html

回答by Asumu Takikawa

You could also use sed for this kind of thing:

您也可以将 sed 用于此类事情:

$ echo "1.1231" | sed -e "s/1\.//"
1231

This is just using sed to run a regular expression search and replace, replacing "1." (with appropriate escaping) with "". It only deletes the first match by default.

这只是使用 sed 运行正则表达式搜索和替换,替换“1”。(带有适当的转义)与“”。默认情况下,它只删除第一个匹配项。

回答by Michael Wild

If you are using bash, you can do this easily with parameter substitution:

如果您正在使用bash,您可以使用参数替换轻松完成此操作:

$ a=1.1231
$ echo ${a#1.}
1231

This will remove the leading"1."string. If you want to remove up to and including the firstoccurrence, use ${a#*1.}and if you want to remove everything up to and including the lastoccurrence, use ${##*1.}.

这将删除前导"1."字符串。如果要删除直到并包括第一次出现的内容,请使用${a#*1.},如果要删除直到并包括最后一次出现的所有内容,请使用${##*1.}

The TLDP page on string manipulationhas further options (such as substring extraction).

关于字符串操作的 TLDP 页面有更多选项(例如子字符串提取)。

Note that using standard shbuilt-in string manipulation tools for such simple transformations will always be much faster than using an external tool, such as sed, awkor cutbecause the shell doesn't have to create a sub-process to perform the operation. However, for more complicated things (e.g. you need to use regular expressions or when the input is large), you're better of using the dedicated tools.

请注意,使用标准sh内置字符串操作工具进行此类简单转换总是比使用外部工具(例如 )快得多sedawk或者cut因为 shell 不必创建子进程来执行操作。但是,对于更复杂的事情(例如,您需要使用正则表达式或当输入很大时),您最好使用专用工具。

回答by tripleee

Since you asked specifically about awk, here is another one.

由于您专门询问了awk,这里是另一个。

awk '{ gsub(/1\./,"") }1' input.txt

As any awktutorial will tell you, the general form of an awkprogram is a sequence of 'condition { actions }'. If you have no actions, the default action is to print. If you have no conditions, the actions will be taken unconditionally. This program uses both of these special cases.

正如任何awk教程都会告诉您的那样,程序的一般形式awk是一系列“条件{动作}”。如果您没有任何操作,则默认操作是打印。如果您没有条件,则将无条件地采取行动。该程序使用这两种特殊情况。

The first part is an action without a condition, i.e. it will be taken for all lines. The action is to substitute all occurrences of the regular expression /1\./with nothing. So this will trim any '1.' (regardless of context) from a line.

第一部分是一个没有条件的动作,即它将对所有行执行。操作是将所有出现的正则表达式/1\./替换为空。所以这将修剪任何“1”。(无论上下文)来自一行。

The second part is a condition without an action, i.e. it will print if the condition is true, and the condition is always true. This is a common idiom for "we are done -- print whatever we have now". It consists simply of the constant 1(which when used as a condition means "true", simply).

第二部分是没有动作的条件,即如果条件为真则打印,并且条件始终为真。这是“我们完成了 - 打印我们现在拥有的任何内容”的常见习语。它仅由常量组成1(当用作条件时,简单地表示“真”)。

This could be reformulated in a number of ways. For example, you could factor the print into the first action;

这可以通过多种方式重新表述。例如,您可以将打印作为第一个操作的因素;

awk '{ gsub(/1\./,""); print }'  input.txt

Perhaps you want to substitute the integer part, i.e. any numbers before a period sign. The regex for that would be something like /[0-9]+\./.

也许您想替换整数部分,即句点符号之前的任何数字。正则表达式将类似于/[0-9]+\./.

gsubis a GNU extension, so you might want to replace it with subor some sort of loop if you need portability to legacy awksyntax.

gsub是 GNU 扩展,因此sub如果您需要对旧awk语法的可移植性,您可能希望将其替换为某种循环。