提取bash/ash中字符串的开头字母。

huangapple go评论66阅读模式
英文:

Extracting beginning alphabets of a string in bash/ash

问题

如何从字符串中提取开头的字母?
我想要提取在第一个非字母字符之前出现的字母。

例如,如果输入字符串是abcd045tj56,输出应该是abcd

同样地,如果输入是jkl657890,输出应该是jkl

可以使用shell脚本中的awk/sed/cut来实现吗?

我尝试了以下代码:

echo "XYZ123" | awk 'sub(/[[:alpha:]]*/, "")'

但是它输出的是123而不是xyz。

然后我尝试了以下代码:

echo "XYZ123" | awk '{print (/[[:alpha:]]*/)}'

但是它输出的是1。

我希望答案是XYZ。

英文:

How can I extract the beginning alphabetic letters from a string?
I want to extract alphabets occurring in the beginning before I hit the first non-alphabetic character.

e.g. If the input string is abcd045tj56 the output should be abcd

Similarly, if the input is jkl657890 the output should be jkl

Can it be done in shell script using awk/sed/cut?

I tried

echo "XYZ123" | awk 'sub(/[[:alpha:]]*/, "")'

But it gives 123 instead of xyz

Then I tried

echo "XYZ123" | awk '{print (/[[:alpha:]]*/)}'

but it gives 1

I want the answer to be XYZ

答案1

得分: 5

将我的评论转换为答案。使用任何awk版本。

awk '
match($0,/^[a-zA-Z]+/){
  print substr($0,RSTART,RLENGTH)
}
' Input_file

或者:

awk '
match($0, /[^[:alpha:]]/){
  print substr($0, 1, RSTART-1)
}
' Input_file
英文:

Converting my comment to an answer here. Using any awk version.

awk '
match($0,/^[a-zA-Z]+/){
  print substr($0,RSTART,RLENGTH)
}
' Input_file

OR:

awk '
match($0, /[^[:alpha:]]/){
  print substr($0, 1, RSTART-1)
}
' Input_file

答案2

得分: 4

你可以使用以下的sed命令:

sed 's/[^[:alpha:]].*$//'

这个sed命令匹配非字母字符及其后面的所有内容,并将其替换为空字符串。

示例:

sed 's/[^[:alpha:]].*$//' <<< 'abcd045tj56'
abcd

sed 's/[^[:alpha:]].*$//' <<< 'XYZ123'
XYZ

sed 's/[^[:alpha:]].*$//' <<< 'jkl657890'
jkl

如果你想在bash中实现同样的效果,可以使用以下命令:

s='abcd045tj56'
echo "${s/[^[:alpha:]]*}"

abcd
英文:

You may use this sed:

sed &#39;s/[^[:alpha:]].*$//&#39;

This sed matches a non-alpha character and everything afterwards and substitutes with an empty string.

Examples:

sed &#39;s/[^[:alpha:]].*$//&#39; &lt;&lt;&lt; &#39;abcd045tj56&#39;
abcd

sed &#39;s/[^[:alpha:]].*$//&#39; &lt;&lt;&lt; &#39;XYZ123&#39;
XYZ

sed &#39;s/[^[:alpha:]].*$//&#39; &lt;&lt;&lt; &#39;jkl657890&#39;
jkl

If you want to do this in bash then:

s=&#39;abcd045tj56&#39;
echo &quot;${s/[^[:alpha:]]*}&quot;

abcd

答案3

得分: 2

使用grep命令:

$ grep -Eo '^[A-Za-z]+' <<< "XYZ123"

只匹配字符串开头的字母字符。

英文:

Use grep:

$ grep -Eo &#39;^[A-Za-z]+&#39; &lt;&lt;&lt;&quot;XYZ123&quot;

to only match alphabetic letters at the beginning of the string.

答案4

得分: 1

你可以使用awk命令,将非字母字符作为字段分隔符,以便通过打印第一个字段来获取前导字母:

awk -F'[^[:alpha:]]' '{print $1}';

演示:https://awk.js.org/?snippet=g7eajb

英文:

You can use awk with a non-alphabet as the field separator so you can get the leading alphabets by printing the first field:

awk -F&#39;[^[:alpha:]]&#39; &#39;{print $1}&#39;

Demo: https://awk.js.org/?snippet=g7eajb

答案5

得分: 1

你可以使用bash的参数扩展来删除第一个非字母字符及其后的所有字符:

s=XYZ123
echo ${s%%[^[:alpha:]]*}

演示:https://onlinegdb.com/OzjGf53T-

请注意,这种方法的性能好处是避免了生成单独进程的开销。

英文:

You can use bash's parameter expansion to remove the first non-alphabet and all characters after it:

s=XYZ123
echo ${s%%[^[:alpha:]]*}

Demo: https://onlinegdb.com/OzjGf53T-

Note that this approach has the performance benefit of avoiding the overhead of spawning a separate process.

答案6

得分: 1

我尝试了以下命令:

echo "XYZ123" | awk '{sub(/[[:alpha:]]*/, ""); print}'

但是它输出的是123而不是xyz。

你使用GNU AWK指令来替换零个或多个字母字符为空字符串。如果你想使用sub函数来完成这个任务,你可以选择非字母字符后面的零个或多个任意字符,即:

echo "XYZ123" | awk '{sub(/[^[:alpha:]].*/, ""); print}'

这样输出结果就是:

XYZ

(在GNU Awk 5.1.0中测试通过)

英文:

> I tried
>
> echo &quot;XYZ123&quot; | awk &#39;sub(/[[:alpha:]]*/, &quot;&quot;)&#39;
>
> But it gives 123 instead of xyz

You instructed GNU AWK to replace zero-or-more alphabetic characters using empty string, if you wish to do this task using sub select non-alpha character followed by zero-or-more any characters, namely

echo &quot;XYZ123&quot; | awk &#39;{sub(/[^[:alpha:]].*/, &quot;&quot;);print}&#39;

gives output

XYZ

(tested in GNU Awk 5.1.0)

答案7

得分: 1

使用gnu awk,您可以打印第一个或多个字母:

echo "XYZ123" | awk 'match($0, /[[:alpha:]]+/, a) {print a[0]}'

输出:

XYZ

如果至少应该有一个非字母字符跟随,您可以使用捕获组并打印该值:

echo "XYZ123" | awk 'match($0, /([[:alpha:]]+)[^[:alpha:]]/, a) {print a[1]}'
英文:

Using gnu awk you can print the first 1 or more alphabetic letters:

echo &quot;XYZ123&quot; | awk &#39;match($0, /[[:alpha:]]+/, a) {print a[0]}&#39;

Output

XYZ

<hr>

If there should be at least a single a non alphabetic character following, you can use a capture group and print that value:

echo &quot;XYZ123&quot; | awk &#39;match($0, /([[:alpha:]]+)[^[:alpha:]]/, a) {print a[1]}&#39;

huangapple
  • 本文由 发表于 2023年7月27日 14:52:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定