匹配后删除文件中的文本

时间:2014-03-03 15:07:26

标签: bash

我有一个包含以下内容的文件:

/home/adversion/web/wp-content/plugins/akismet/index1.php: PHP.Mailer-7 FOUND
/home/beckydodman/web/oldshop/images/google68274020601e.php: Trojan.PHP-1 FOUND
/home/resurgence/web/Issue 272/Batch 2 for Helen/keynote_Philip Baldwin (author revise).doc: W97M.Thus.A FOUND
/home/resurgence/web/Issue 272/from Helen/M keynote_Philip Baldwin.doc: W97M.Thus.A FOUND
/home/skda/web/clients/sandbox/wp-content/themes/editorial/cache/external_dc8e1cb5bf0392f054e59734fa15469b.php: Trojan.PHP-58 FOUND

我需要通过删除冒号(:)之后的所有内容来清理此文件。

所以它看起来像这样:

/home/adversion/web/wp-content/plugins/akismet/index1.php
/home/beckydodman/web/oldshop/images/google68274020601e.php
/home/resurgence/web/Issue 272/Batch 2 for Helen/keynote_Philip Baldwin (author revise).doc
/home/resurgence/web/Issue 272/from Helen/M keynote_Philip Baldwin.doc
/home/skda/web/clients/sandbox/wp-content/themes/editorial/cache/external_dc8e1cb5bf0392f054e59734fa15469b.php

4 个答案:

答案 0 :(得分:3)

使用awk:

$ awk -F: '{print $1}' input
/home/adversion/web/wp-content/plugins/akismet/index1.php
/home/beckydodman/web/oldshop/images/google68274020601e.php
/home/resurgence/web/Issue 272/Batch 2 for Helen/keynote_Philip Baldwin (author revise).doc
/home/resurgence/web/Issue 272/from Helen/M keynote_Philip Baldwin.doc
/home/skda/web/clients/sandbox/wp-content/themes/editorial/cache/external_dc8e1cb5bf0392f054e59734fa15469b.php

cut

$ cut -d: -f1 input

sed

$ sed 's/:.*$//' input
在awk-mode中

perl

$ perl -F: -lane 'print $F[0]' input

最后,纯bash

#!/bin/bash

while read line
do
    echo ${line%%:*}
done < input

答案 1 :(得分:1)

这应该足够了

awk -F: '{print $1}' file-name

答案 2 :(得分:1)

这里没有sed / awk解决方案

 cut -d : -f 1 [filename]

答案 3 :(得分:0)

通过sed管道:

$ echo "/home/adversion/web/wp-content/plugins/akismet/index1.php: PHP.Mailer-7 FOUND" | sed 's/: .*$//'

/home/adversion/web/wp-content/plugins/akismet/index1.php

只要': '没有出现两次,就会有效。请注意,上面的awk / cut示例更有可能失败,因为它们匹配':'不是':'

相关问题