使用sed将单词/字符串转换为字符列表。

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

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]]*&amp;\ +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 &quot;${charList}&quot; | awk &#39;BEGIN{
		regExp=&quot;&quot; ;
	}
	{
		if( $0 != &quot;&quot; ){
			for( i=1 ; i &lt;= NF ; i++ ){
				regExp=sprintf(&quot;%s[%s%s]&quot;, regExp, toupper($i), tolower($i) ) ;
			} ;
		} ;
	}END{
		printf(&quot;%s\n&quot;, regExp ) ;
	}&#39;
}

explodeString()
{
	charList=$(echo &quot;${pattern}&quot; | sed &#39;s+[[alphanum]]*+&amp;\ +g&#39; )
}


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 &#39;s/([[:alpha:]])|[[:digit:]]/[\u\l&amp;]/g&#39; 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 &#39;s/[[:alnum:]]/\u&amp;\l&amp;/g&#39; 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 &quot;ab*c1&quot; &quot;d2e?f&quot;
do  echo &quot;${pattern}&quot; |
    sed &#39;s/[[:alpha:]]/[\U&amp;\L&amp;]/g&#39;
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 &quot;${charList}&quot; | awk &#39;BEGIN{
		regExp=&quot;&quot; ;
	}
	{
		if( $0 != &quot;&quot; ){
			for( i=1 ; i &lt;= NF ; i++ ){
				if( $i ~ /[[:alpha:]]/ ){
					regExp=sprintf(&quot;%s[%s%s]&quot;, regExp, toupper($i), tolower($i) ) ;
				}else{
					regExp=sprintf(&quot;%s%s&quot;, regExp, $i ) ;
				} ;
			} ;
		} ;
	}END{
		printf(&quot;%s\n&quot;, regExp ) ;
	}&#39;
}

explodeString()
{
	charList=$(echo &quot;${pattern}&quot; | sed &#39;s+.+&amp;\ +g&#39; )
}


for pattern in &quot;$@&quot;
do
	explodeString
	makePatternMatch
done

Thank again to all who provided various examples and insights!

huangapple
  • 本文由 发表于 2023年2月27日 11:30:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576557.html
匿名

发表评论

匿名网友

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

确定