如何從每一行中刪除尾隨的非字母字元?

如何從每一行中刪除尾隨的非字母字元?

我正在嘗試刪除除字母之外的最後一個字元:

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

答案4

這是一個經典的正規表示式搜尋和替換https://regex101.com/r/gRiUTc/2

透過 shell 你可以使用

<input sed -r 's/(\W+|[0-9]+)$//g'

相關內容