【问题标题】:Remove e-mail string from the beginning of each row in a file从文件中每一行的开头删除电子邮件字符串
【发布时间】:2015-05-28 19:05:38
【问题描述】:

我有一个格式如下的文件

email1@email.com some string with no set width
email2@email.com another string
email3@email.com yet another string!!
areallylongemail@email.com shortstring

我想要做的是运行一个 bash 命令,该命令将获取此文件并从每行的开头删除电子邮件+空格并将其输出到另一个文件。输出文件应该是

some string with no set width
another string
yet another string!!
shortstring

到目前为止,我有sed 's/^\S+\s//' test_input.txt > test_output.txt,但似乎不起作用....

【问题讨论】:

    标签: linux shell command-line-interface cut


    【解决方案1】:

    你可以使用sed:

    sed 's/^[^[:blank:]@]\+@[^[:blank:]]\+[[:blank:]]*//' file > file.out
    some string with no set width
    another string
    yet another string!!
    shortstring
    

    gnu sed 可以使用:

    sed 's/^[^\s@]\+@\S\+\s*//' file > file.out
    

    【讨论】:

    • $ sed 's/^[^\s@]\+@\S\+\s*//' test_input.txt email1@email.com some string with no set width email2@email.com another string email3@email.com yet another string!! areallylongemail@email.com shortstring 由于某种原因无法正常工作
    • 一行也可以有多个电子邮件地址吗?它适用于您提供的数据,如果您使用无效的数据编辑问题,我可以调查一下。
    • 不是只有一个e-mail地址,而且我使用了和上面这个问题一样的输入。
    • 我认为你需要's/regex1/regex2/g',因为你想替换文件中所有出现的电子邮件。您也可以使用 -i 标志来执行此操作。
    • [[:blank:]] 是匹配空格或制表符的 POSIX 类。第二个命令是 gnu-sed 特定的,所以你的 sed 可能有点老。
    【解决方案2】:

    如果电子邮件地址与行的其余部分之间只有一个空格,那么您可以使用cut 命令,告诉它使用空格作为字段分隔符,并让它输出所有以第二个字段。

    cut -d' ' -f2- < file > file.out
    

    【讨论】:

      猜你喜欢
      • 2020-06-04
      • 2020-01-12
      • 1970-01-01
      • 2014-05-21
      • 2012-02-08
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      相关资源
      最近更新 更多