sed 或 awk 中的字符串大写 [英] Capitalize strings in sed or awk

查看:19
本文介绍了sed 或 awk 中的字符串大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 bash 脚本中使用三种类型的字符串.我认为 sed/awk 将是我最好的选择,但我不确定.鉴于以下要求,最好的方法是什么?

I have three types of strings that I'd like to capitalize in a bash script. I figured sed/awk would be my best bet, but I'm not sure. What's the best way given the following requirements?

  1. 单字
    例如<代码>炸玉米饼 ->炸玉米饼

多个单词用连字符分隔
例如my-fish-tacos ->我的鱼炸玉米饼

多个单词用下划线分隔
例如my_fish_tacos ->My_Fish_Tacos

推荐答案

没有必要使用捕获组(尽管 & 在某种程度上是一个):

There's no need to use capture groups (although & is a one in a way):

echo "taco my-fish-tacos my_fish_tacos" | sed 's/[^ _-]*/u&/g'

输出:

Taco My-Fish-Tacos My_Fish_Tacos

转义的小写u"将匹配子字符串中的下一个字符大写.

The escaped lower case "u" capitalizes the next character in the matched sub-string.

这篇关于sed 或 awk 中的字符串大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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