2014-03-03 87 views
-2

後,我有以下文件:刪除文件中的文本匹配

/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 
+1

您是否可以包含您編寫的代碼以嘗試解決此問題? –

回答

1

這應該足以

awk -F: '{print $1}' file-name 
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 

perl在AWK模式

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

最後,純bash

#!/bin/bash 

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

這裏一個無SED/AWK溶液

cut -d : -f 1 [filename] 
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更可能因符合':'not'而失敗:'

+1

首先,由於輸入本身就是一個文件,因此不需要管道輸入。 第二個問題清楚地表明他想刪除在''之後出現的任何內容:''而不是'':'' –