如何使用bash清除表情符号中的文本

时间:2016-05-21 05:58:12

标签: bash awk sed

有没有办法使用awk或sed从文本中删除任何不是令牌,标点符号或特殊字符的内容?我真正想要摆脱的是表情符号和类似的符号。

示例输入:

Si tú no estáss yo no voy a lloraar por tiii
Me respondes porfavor?? piensas venir a Ecuador
cosas veredes!!!! Ay Papá.
what y'all know about this?
‼️ ‼️ tag  they make the final decision
Vähän on twiitattavaa muuta kuin että aijjai ja oijjoi sekä nannaa. 
Binta On est arrivé au chicken elle voulait pleuré carrément tellement elle était heureuse
ja mir fällt nix mehr ein
Někdo v pátek semnou na flédu na Moju reč???

示例输出:

{{1}}

3 个答案:

答案 0 :(得分:1)

我最好的解决方案是使用Python,Python文件必须是UTF-8。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re

text = u"""Si tú no estáss yo no voy a lloraar por tiii
Me respondes porfavor?? ❤ piensas venir a Ecuador
cosas veredes!!!! Ay Papá. 
   what y'all know about this?
❤️‼️  ❤️‼️ tag  they make the final decision 
Vähän on twiitattavaa muuta kuin että aijjai ja oijjoi sekä nannaa. 
Binta On est arrivé au chicken elle voulait pleuré carrément tellement elle était heureuse 
ja mir fällt nix mehr ein
Někdo v pátek semnou na flédu na Moju reč???
"""

emoji_pattern = re.compile(
    "["
    u"\U0001F600-\U0001F64F"  # emoticons
    u"\U0001F300-\U0001F5FF"  # symbols & pictographs
    u"\U0001F680-\U0001F6FF"  # transport & map symbols
    u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
    u"\U00002760-\U0000276F"  # emoticons
    "]+", flags=re.UNICODE
)

print(emoji_pattern.sub(r'', text))

停止

Si tú no estáss yo no voy a lloraar por tiii
Me respondes porfavor??  piensas venir a Ecuador
cosas veredes!!!! Ay Papá. 
   what y'all know about this?
‼️  ️‼️ tag  they make the final decision 
Vähän on twiitattavaa muuta kuin että aijjai ja oijjoi sekä nannaa. 
Binta On est arrivé au chicken elle voulait pleuré carrément tellement elle était heureuse 
ja mir fällt nix mehr ein
Někdo v pátek semnou na flédu na Moju reč???

答案 1 :(得分:0)

此命令将删除不是字母,数字,标点符号或空格的每个字符:

sed 's/[^[:alnum:][:punct:][:space:]]//g' input

限制:请注意,您看到的某些有趣字符可能是您的计算机缺少已安装字体的有效unicode字母字符。这不会删除它们。

如何运作

[:alnum:][:punct:][:space:]是分别匹配任何字母数字,标点符号或空格字符的字符类。正则表达式[^[:alnum:][:punct:][:space:]]匹配任何不属于这三个类之一的字符。 sed替换命令s/[^[:alnum:][:punct:][:space:]]//g执行全局搜索和替换,查找不在其中一个类中的任何字符,并将其替换为空,即删除它。

答案 2 :(得分:0)

您可以使用tr

% tr -dc '[:print:]' < emoji.txt
Si t no estss yo no voy a lloraar por tiiiMe respondes porfavor??  piensas venir a Ecuadorcosas veredes!!!! Ay Pap.    what y'all know about this?   tag  they make the final decision Vhn on twiitattavaa muuta kuin ett aijjai ja oijjoi sek nannaa. Binta On est arriv au chicken elle voulait pleur carrment tellement elle tait heureuse ja mir fllt nix mehr einNkdo v ptek semnou na fldu na Moju re??? 

正如您所看到的,这也将删除换行符,可以通过以下方式阻止:

% tr -dc '[:print:]\n' < emoji.txt
Si t no estss yo no voy a lloraar por tiii
Me respondes porfavor??  piensas venir a Ecuador
cosas veredes!!!! Ay Pap. 
   what y'all know about this?
   tag  they make the final decision 
Vhn on twiitattavaa muuta kuin ett aijjai ja oijjoi sek nannaa. 
Binta On est arriv au chicken elle voulait pleur carrment tellement elle tait heureuse 
ja mir fllt nix mehr ein
Nkdo v ptek semnou na fldu na Moju re???