提取一个字符串的前两个字符(Shell脚本) [英] Extracting first two characters of a string (Shell Scripting)

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

问题描述

我是新来的sed和awk - 所以我真的不知道这是去这个问题的最有效方法。

我期待提取字符串的前两个字母。我能做到这一点,如果他们要每次都一样,但我似乎无法弄清楚如何,只是说,

从拿这个更大的字符串此字符串的n个位置 X

IE浏览器。


USCAGoleta9311734.5021-120.1287855805 = US


解决方案

可能是最有效的方法,如果你使用了庆典壳(你似乎是根据您的评论),是使用参数扩展的子变种:

 长= USCAGol ...
短= $ {长:0:2}
回声$ {}短

这将设置的前两个字符长。如果少于两个字符,将是相同的吧。

这是通常更好,如果你打算因为没有进程创建开销做了很多(如每份报告50000次你提到)。它们使用外部程序所有的解决方案将遭受来自开销。

如果您还希望确保的最小的长度,你可以垫出来的前手的东西,如:

  tmpstr =$ {}长。
短= $ {tmpstr:0:2}

这将确保任何长度小于两个字符是与时间正确的填充(或别的东西,只是通过改变创建时使用的字符 tmpstr )。目前尚不清楚,你需要这一点,但我想我把它的完整性。


话虽如此,有很多方式与外部程序(比如,如果你没有庆典提供给你)要做到这一点,其中一些分别是:

 短= $(回声$ {长} |切-c1-2)
短= $(回声$ {长} |头-C2)
短= $(回声$ {长} | awk的'{打印SUBSTR($ 0,0,2)}
短= $(回声$ {长} | sed的'S / ^ \\(.. \\)* / \\ 1 /')

前两个(削减)是单行线相同的 - 他们基本上都只有给你回的前两个字符。他们在切割不同会给你每行前两个字符和会给你的前两个字符整个输入的

第三个使用 AWK substring函数提取前两个字符和第四使用 SED 捕获组(使用() \\ 1 )来捕获前两个字符,并与他们更换整个行。他们都是类似于剪切 - 他们每行前两个字符传递在输入

那些都不重要,如果你确定你输入的是单行线,它们都具有相同的效果。

I am new to sed and awk - so I am not really sure which is the most efficient way to go about this.

I am looking to extract the first two letters of a string. I could do it if they were going to be same every time, but I can't seem to figure out how to just say,

Take n positions of this string from this larger string x.

IE.

USCAGoleta9311734.5021-120.1287855805 = US

解决方案

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 substring variant of parameter expansion:

long=USCAGol...
short=${long:0:2}
echo ${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.

This 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:

tmpstr="${long}.."
short=${tmpstr:0:2}

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.


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/')

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

The third one uses the awk substring 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天全站免登陆