我的文件包含大量文字,我想要删除所有字母数字。
Example of words to be removed:
gr8
2006
sdlfj435ljsa
232asa
asld213
ladj2343asda
asd!32
我能做到这一点的最佳方式是什么?
答案 0 :(得分:6)
如果你想删除所有由字母和数字组成的单词,只留下由所有数字或所有字母组成的单词:
sed 's/\([[:alpha:]]\+[[:digit:]]\+[[:alnum:]]*\|[[:digit:]]\+[[:alpha:]]\+[[:alnum:]]*\) \?//g' inputfile
示例:
$ echo 'abc def ghi 111 222 ab3 a34 43a a34a 4ab3' | sed 's/\<\([[:alpha:]]\+[[:digit:]]\+[[:alnum:]]*\|[[:digit:]]\+[[:alpha:]]\+[[:alnum:]]*\) \?//g'
abc def ghi 111 222
答案 1 :(得分:2)
假设您希望从示例文本中获得的唯一输出是2006
,并且每行有一个单词:
sed '/[[:alpha:]]\+/{/[[:digit:]]\+/d}' /path/to/alnum/file
$ cat alnum
gr8
2006
sdlFj435ljsa
232asa
asld213
ladj2343asda
asd!32
alpha
$ sed '/[[:alpha:]]\+/{/[[:digit:]]\+/d}' ./alnum
2006
alpha
答案 2 :(得分:0)
如果目标实际上是删除所有字母数字单词(完全由字母和数字组成的字符串),则此sed
命令将起作用。它没有替换所有字母数字字符串。
sed 's/[[:alnum:]]*//g' < inputfile
请注意,alnum
以外的其他字符类也可用(请参阅man 7 regex
)。
对于给定的示例数据,这只留下6个空行和一个!
(因为这是示例数据中唯一的非字母数字字符)。这实际上是你想要做的吗?
答案 3 :(得分:0)
AWK解决方案:
BEGIN { # Statement that will be executed once at the beginning.
FS="[ \t]" # Set space and tab characters to be treated as word separator.
}
# Code below will execute for each line in file.
{
x=1 # Set initial word index to 1 (0 is the original string in array)
fw=1 # Indicate that future matched word is a first word. This is needed to put newline and spaces correctly.
while ( x<=NF )
{
gsub(/[ \t]*/,"",$x) # Strip word. Remove any leading and trailing white-spaces.
if (!match($x,"^[A-Za-z0-9]*$")) # Print word only if it does not match pure alphanumeric set of characters.
{
if (fw == 0)
{
printf (" %s", $x) # Print the word offsetting it with space in case if this is not a first match.
}
else
{
printf ("%s", $x) # Print word as is...
fw=0 # ...and indicate that future matches are not first occurrences
}
}
x++ # Increase word index number.
}
if (fw == 0) # Print newline only if we had matched some words and printed something.
{
printf ("\n")
}
}
假设您在script.awk' and data in
data.txt , you have to invoke
awk中设置了此脚本:
awk -f ./test.awk ./data.txt
对于您的文件,它将产生:
asd!32
对于像这样的更复杂的案例:
gr8
2006
sdlfj435ljsa
232asa he!he lol
asld213 f
ladj2343asda
asd!32 ab acd!s
......它会产生这个:
he!he
asd!32 acd!s
希望它有所帮助。 祝你好运!