如何在Shell脚本中提取字符串的前两个字符

如何在Shell脚本中提取字符串的前两个字符

本文介绍了如何在Shell脚本中提取字符串的前两个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,给定:

USCAGoleta9311734.5021-120.1287855805

我只想提取:

US

推荐答案

如果您使用的是bash shell(根据您的评论似乎是这样),可能是最有效的方法.参数扩展的子字符串变体:

Probably the most efficient method, if you're using the bash shell (and you appear to be, based on your comments), is to use the sub-string variant of parameter expansion:

pax> long="USCAGol.blah.blah.blah"
pax> short="${long:0:2}" ; echo "${short}"
US

这会将short设置为long的前两个字符.如果long少于两个字符,则short将与它相同.

This will set short to be the first two characters of long. If long is shorter than two characters, short will be identical to it.

如果您要进行很多操作(如您提到的每个报告50,000次),则这种内嵌方法通常会更好,因为没有流程创建开销.所有使用外部程序的解决方案都将遭受这些开销.

This in-shell method is usually better if you're going to be doing it a lot (like 50,000 times per report as you mention) since there's no process creation overhead. All solutions which use external programs will suffer from that overhead.

如果您还想确保最小长度 ,则可以在手之前将其填充,例如:

If you also wanted to ensure a minimum length, you could pad it out before hand with something like:

pax> long="A"
pax> tmpstr="${long}.."
pax> short="${tmpstr:0:2}" ; echo "${short}"
A.

这将确保在右边用句点填充长度少于两个字符的任何内容(或其他,只需更改创建tmpstr时使用的字符)即可.尚不清楚您是否需要此功能,但我想我将其放入其中以保持完整性.

This would ensure that anything less than two characters in length was padded on the right with periods (or something else, just by changing the character used when creating tmpstr). It's not clear that you need this but I thought I'd put it in for completeness.

话虽如此,但是有多种方法可以使用外部程序(例如,如果您没有bash可用),其中一些是:

Having said that, there are any number of ways to do this with external programs (such as if you don't have bash available to you), some of which are:

short=$(echo "${long}" | cut -c1-2)
short=$(echo "${long}" | head -c2)
short=$(echo "${long}" | awk '{print substr ($0, 0, 2)}'
short=$(echo "${long}" | sed 's/^\(..\).*/\1/')

对于单行字符串,前两个(cuthead)是相同的-基本上,它们都只返回了前两个字符.它们的不同之处在于,cut将为您提供每行的前两个字符,而head将为您提供整个输入的前两个字符

The first two (cut and head) are identical for a single-line string - they basically both just give you back the first two characters. They differ in that cut will give you the first two characters of each line and head will give you the first two characters of the entire input

第三个使用awk子字符串功能提取前两个字符,第四个使用sed捕获组(使用()\1)捕获前两个字符并替换整个字符与他们保持一致.它们都与cut相似-它们在输入中提供每行的前两个字符.

The third one uses the awk sub-string function to extract the first two characters and the fourth uses sed capture groups (using () and \1) to capture the first two characters and replace the entire line with them. They're both similar to cut - they deliver the first two characters of each line in the input.

如果您确定输入内容仅一行,那么它们都具有相同的效果.

None of that matters if you are sure your input is a single line, they all have an identical effect.

这篇关于如何在Shell脚本中提取字符串的前两个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:51