Bash 中的字符串操作

发布于 2024-09-02 18:04:55 字数 359 浏览 5 评论 0原文

我是 Bash 的新手,我正在做一些字符串操作。

我的目录中的其他文件中有以下文件:

jdk-6u20-solaris-i586.sh

我正在执行以下操作以在脚本中获取 jdk-6u20:

myvar=`ls -la | awk '{print $9}' | egrep "i586" | cut -c1-8`
echo $myvar

但现在我想将 jdk-6u20 转换为 jdk1.6.0_20。我似乎不知道该怎么做。

它必须尽可能通用。例如,如果我有 jdk-6u25,我应该能够以相同的方式将其转换为 jdk1.6.0_25 等等

有什么建议吗?

I am a newbie in Bash and I am doing some string manipulation.

I have the following file among other files in my directory:

jdk-6u20-solaris-i586.sh

I am doing the following to get jdk-6u20 in my script:

myvar=`ls -la | awk '{print $9}' | egrep "i586" | cut -c1-8`
echo $myvar

but now I want to convert jdk-6u20 to jdk1.6.0_20. I can't seem to figure out how to do it.

It must be as generic as possible. For example if I had jdk-6u25, I should be able to convert it at the same way to jdk1.6.0_25 so on and so forth

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

悲凉≈ 2024-09-09 18:04:55

根据您想要的通用程度以及输入的标准程度,您可能可以使用 AWK 来完成所有操作。通过使用 FS="regexp" 指定字段分隔符,您可以通过任何最有意义的标记分解原始字符串,并使用 printf 将它们按任意顺序重新组合在一起>。

例如,假设破折号和字母“u”仅用于分隔字段:

myvar="jdk-6u20-solaris-i586.sh"
echo $myvar | awk 'BEGIN {FS="[-u]"}; {printf "%s1.%s.0_%s",$1,$2,$3}'

根据口味调味。

Depending on exactly how generic you want it, and how standard your inputs will be, you can probably use AWK to do everything. By using FS="regexp" to specify field separators, you can break down the original string by whatever tokens make the most sense, and put them back together in whatever order using printf.

For example, assuming both dashes and the letter 'u' are only used to separate fields:

myvar="jdk-6u20-solaris-i586.sh"
echo $myvar | awk 'BEGIN {FS="[-u]"}; {printf "%s1.%s.0_%s",$1,$2,$3}'

Flavour according to taste.

吃兔兔 2024-09-09 18:04:55

仅使用 Bash:

for file in jdk*i586*
do
    file="${file%*-solaris*}"
    file="${file/-/1.}"
    file="${file/u/.0_}"
    do_something_with "$file"
done

Using only Bash:

for file in jdk*i586*
do
    file="${file%*-solaris*}"
    file="${file/-/1.}"
    file="${file/u/.0_}"
    do_something_with "$file"
done
挽容 2024-09-09 18:04:55

我认为

sed

is the command for you

i think that

sed

is the command for you

薯片软お妹 2024-09-09 18:04:55

你可以尝试这个片段:

for fname in *; do
    newname=`echo "$fname" | sed 's,^jdk-\([0-9]\)u\([0-9][0-9]*\)-.*$,jdk1.\1.0_\2,'`
    if [ "$fname" != "$newname" ]; then
        echo "old $fname, new $newname"
    fi
done

You can try this snippet:

for fname in *; do
    newname=`echo "$fname" | sed 's,^jdk-\([0-9]\)u\([0-9][0-9]*\)-.*$,jdk1.\1.0_\2,'`
    if [ "$fname" != "$newname" ]; then
        echo "old $fname, new $newname"
    fi
done
抚你发端 2024-09-09 18:04:55
awk 'if(match($9,"i586")){gsub("jdk-6u20","jdk1.6.0_20");print $9;}'

如果您想使用 if(match()),则它会取代 egrep 位。您也可以使用 substr($9,1,8) 而不是 cut 。

awk 'if(match($9,"i586")){gsub("jdk-6u20","jdk1.6.0_20");print $9;}'

The if(match()) supersedes the egrep bit if you want to use it. You could use substr($9,1,8) instead of cut as well.

只涨不跌 2024-09-09 18:04:55

garph0 对 sed 有一个好主意;你可以做

myvar=`ls jdk*i586.sh | sed 's/jdk-\([0-9]\)u\([0-9]\+\).\+$/jdk1.\1.0_\2/'`

garph0 has a good idea with sed; you could do

myvar=`ls jdk*i586.sh | sed 's/jdk-\([0-9]\)u\([0-9]\+\).\+$/jdk1.\1.0_\2/'`
太阳公公是暖光 2024-09-09 18:04:55

您需要 awk,因为 ls 上有 -l 开关。对于文本行的模式替换,sed 是长期的冠军:

ls | sed -n '/^jdk/s/jdk-\([0-9][0-9]*\)u\([0-9][0-9]*\)$/jdk1.\1.0_\2/p'

这是用“老式”sed 编写的,应该具有更好的跨平台可移植性。表达式表示:

  • 除非它们与以 'jdk' 开头的行上的 -n 匹配,否则不要打印行
  • do:
  • 在仅包含“jdk-IntegerAuIntegerB”的行上
    • 将其更改为“jdk.1.IntegerA.0_IntegerB”
    • 并打印

您的示例变得更加简单:

myvar=`echo *solaris-i586.sh | sed 's/-solaris-i586\.sh//'`

You're needing the awk in there is an artifact of the -l switch on ls. For pattern substitution on lines of text, sed is the long-time champion:

ls | sed -n '/^jdk/s/jdk-\([0-9][0-9]*\)u\([0-9][0-9]*\)$/jdk1.\1.0_\2/p'

This was written in "old-school" sed which should have greater portability across platforms. The expression says:

  • don't print lines unless they match -n
  • on lines beginning with 'jdk' do:
  • on a line that contains only "jdk-IntegerAuIntegerB"
    • change it to "jdk.1.IntegerA.0_IntegerB"
    • and print it

Your sample becomes even simpler as:

myvar=`echo *solaris-i586.sh | sed 's/-solaris-i586\.sh//'`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文