我正在尝试删除字母以外的最后一个字符:
support.help1.com,,
support.help1.com.
support.help1.com9
support.help1.com*
support.help1.com@@
support.help1.com##
support.help1.com%%
support.help1.com^
support.help1.com
support.help1.com,
support.help1.com-
我希望我的输出像这样:
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
答案1
sed 也可能有帮助:
command | sed 's/[^a-Z]*$//g'
# create the example output
$ echo "support.help1.com,,
support.help1.com.
support.help1.com9
support.help1.com*
support.help1.com@@
support.help1.com##
support.help1.com%%
support.help1.com^
support.help1.com
support.help1.com,
support.help1.com-" > trailexample.txt
# now edit this stream
# something like $ command_output | sed
$ cat trailexample.txt | sed 's/[^a-Z]*$//g'
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
# explanation
# sed (replace) 's/this/by-this/g' :: sed 's/[^a-Z]*$//g'
# s : substitution command, we want to substitute strings
# The 'this' [^a-Z]*$ : regexp pattern
# ^ mean not
# a-Z means all aLphBetiCaL chars
# []* any number of what is in brackets
# $ means end of line
# So the 'this' is 'any number of consecutive non-alphabetical chars before end of line'
# And the 'by-this' is empty, nothing, nada, void :: //
# g : global substitution command, means do the replacement for all occurrences
答案2
如果你可以使用正则表达式,只需加载每个命令并使用下面的正则表达式(从这里):
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
此正则表达式接受带有http
/ 的URL https
。只需使用它来确认您的 URL 是否有效,如果无效,只需通过删除最后一个字符来加载字符串。您可以使用这个解决方法为了那个原因:
string="string.help1.com&&"
foo=string
while [ !regex(foo) ]; do
foo=${foo%?}
done
print foo
注意:regex(foo)
只是获取字符串的函数,True
如果正则表达式正确则返回,False
在其他情况下
NB2:我的语法可能不正确,但这只是给你一个提示
答案3
您可以使用 perl 单行命令来实现此目的:
perl -pne 's/[^a-zA-Z]*$/\n/g' input.txt
这将逐行读取内容,并将行末的input.txt
所有非字母字符 ( ) 替换为换行符 ( )[^a-zA-Z]*$
\n