linux – 如何在bash中提取一个子字符串

linux – 如何在bash中提取一个子字符串,第1张

概述我在bash中有以下字符串,长度> 4 str = "abcdefghijklmno" 我想提取到str2的第一个第一个char的字符串.所以 str2="abcde" 如何用bash? 请使用表达式 {string:position:length} 所以在这种情况下 $str="abcdefghijklm"$echo "${str:0:5}"abcde 查看其他用法: $echo "${st 我在bash中有以下字符串,长度> 4
str = "abcdefghijklmno"

我想提取到str2的第一个第一个char的字符串.所以

str2="abcde"

如何用bash?

解决方法 请使用表达式
{string:position:length}

所以在这种情况下

$str="abcdefghijklm"$echo "${str:0:5}"abcde

查看其他用法:

$echo "${str:0}"      # default: start from the 0th positionabcdefghijklm$echo "${str:1:5}"    # start from the 1th and get 5 charactersbcdef$echo "${str:10:1}"   # start from 10th just one characterk$echo "${str:5}"      # start from 5th until the endfghijklm

取自:
– wooledge.org – How can I use parameter expansion? How can I get substrings? How can I get a file without its extension,or get just a file’s extension?
– Shell Command Language – 2.6.2 Parameter Expansion

总结

以上是内存溢出为你收集整理的linux – 如何在bash中提取一个子字符串全部内容,希望文章能够帮你解决linux – 如何在bash中提取一个子字符串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1048952.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存