如何在 shell 脚本中提取字符串的前两个字符? [英] How can I extract the first two characters of a string in shell scripting?

查看:131
本文介绍了如何在 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 脚本中提取字符串的前两个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆