英文:
Use sed to convert word/string into list of characters
问题
以下是您要的中文翻译:
希望有一个通用的实用工具,可以根据命令行参数动态创建正则表达式模式。
以下脚本仅用于演示分割和转换的方法,不是最终产品的示例。注意: 是的,我打算在 [tag:sed] 中使用 [[alphanum]],而不是 [[alpha]]。
我的问题是,对于命令
scriptname abc
输出是
[Aa][BCbc]
但我希望它是
[Aa][Bb][Cc]
我正在寻找正确的sed语法,以指导命令对每个字符单独重复替换,而不仅仅是第一个。
脚本:
#!/bin/bash
makePatternMatch()
{
echo "${charList}" | awk 'BEGIN{
regExp="" ;
}
{
if( $0 != "" ){
for( i=1 ; i <= NF ; i++ ){
regExp=sprintf("%s[%s%s]", regExp, toupper($i), tolower($i) ) ;
} ;
} ;
}END{
printf("%s\n", regExp ) ;
}'
}
explodeString()
{
charList=$(echo "${pattern}" | sed 's+[[alphanum]]*&\ +g' )
}
for pattern in $@
do
explodeString
makePatternMatch
done
英文:
Would like a generalized utility for creating regexp patterns on demand, based on command line parameter.
The following script is only intended to demonstrate the method of splitting and conversion. It is not the final product of how it will be used. NOTE: Yes, I did intend to use [[alphanum]] in [tag:sed], not [[alpha]].
My problem is that the output of the script (included below) for the command
scriptname abc
is
[Aa][BCbc]
when I want it to be
[Aa][Bb][Cc]
I am looking for the correct sed syntax directing the command repeating the substitution for each character individually, not only the first one.
Script:
#!/bin/bash
makePatternMatch()
{
echo "${charList}" | awk 'BEGIN{
regExp="" ;
}
{
if( $0 != "" ){
for( i=1 ; i <= NF ; i++ ){
regExp=sprintf("%s[%s%s]", regExp, toupper($i), tolower($i) ) ;
} ;
} ;
}END{
printf("%s\n", regExp ) ;
}'
}
explodeString()
{
charList=$(echo "${pattern}" | sed 's+[[alphanum]]*+&\ +g' )
}
for pattern in $@
do
explodeString
makePatternMatch
done
答案1
得分: 2
这可能适用于你(GNU sed):
sed -E 's/([[:alpha:]])|[[:digit:]]/[\u\l&]/g' file
如果一个字符是字母或数字,将其替换为[x]
,其中x是数字或大写和小写字母之一。
另一个选择:
sed 's/[[:alnum:]]/\u&\l&/g' file
然而,这会使每个数字重复一次(没有效果)。
英文:
This might work for you (GNU sed):
sed -E 's/([[:alpha:]])|[[:digit:]]/[\u\l&]/g' file
If a character is either alpha or digit replace it by [x]
where x is either a digit or an uppercase and lowercase alpha.
Alternative:
sed 's/[[:alnum:]]/\u&\l&/g' file
However this doubles every digit (no effect).
答案2
得分: 0
以下是翻译好的部分:
Combining input from everyone who replied, I was able to formulate the [tag:sed]-based one-liner command that I was looking for. Namely:
从所有回复者的意见中汇总,我能够制定我所寻找的基于 [tag:sed] 的一行命令。具体来说:
for pattern in "ab*c1" "d2e?f"
do echo "${pattern}" |
sed 's/[[:alpha:]]/[\U&\L&]/g'
done
giving me
给我的结果是
[Aa][Bb]*[Cc]1
[Dd]2[Ee]?[Ff]
But getting back to script in my OP, the modified logic would have been:
但回到我原帖中的脚本,修改后的逻辑将会是:
#!/bin/bash
makePatternMatch()
{
echo "${charList}" | awk 'BEGIN{
regExp="" ;
}
{
if( $0 != "" ){
for( i=1 ; i <= NF ; i++ ){
if( $i ~ /[[:alpha:]]/ ){
regExp=sprintf("%s[%s%s]", regExp, toupper($i), tolower($i) ) ;
}else{
regExp=sprintf("%s%s", regExp, $i ) ;
} ;
} ;
} ;
}END{
printf("%s\n", regExp ) ;
}';
}
explodeString()
{
charList=$(echo "${pattern}" | sed 's+.+& +g' )
}
for pattern in "$@"
do
explodeString
makePatternMatch
done
Thank again to all who provided various examples and insights!
再次感谢所有提供各种示例和见解的人!
英文:
Combining input from everyone who replied, I was able to formulate the [tag:sed]-based one-liner command that I was looking for. Namely:
for pattern in "ab*c1" "d2e?f"
do echo "${pattern}" |
sed 's/[[:alpha:]]/[\U&\L&]/g'
done
giving me
[Aa][Bb]*[Cc]1
[Dd]2[Ee]?[Ff]
But getting back to script in my OP, the modified logic would have been:
#!/bin/bash
makePatternMatch()
{
echo "${charList}" | awk 'BEGIN{
regExp="" ;
}
{
if( $0 != "" ){
for( i=1 ; i <= NF ; i++ ){
if( $i ~ /[[:alpha:]]/ ){
regExp=sprintf("%s[%s%s]", regExp, toupper($i), tolower($i) ) ;
}else{
regExp=sprintf("%s%s", regExp, $i ) ;
} ;
} ;
} ;
}END{
printf("%s\n", regExp ) ;
}'
}
explodeString()
{
charList=$(echo "${pattern}" | sed 's+.+&\ +g' )
}
for pattern in "$@"
do
explodeString
makePatternMatch
done
Thank again to all who provided various examples and insights!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论