2016-11-15 82 views
1

我試圖找到並使用可變用的sed和awk的幫助,但沒有得到任何輸出替換值 -如何使用變量來使用sed或awk進行搜索和替換?

#cat temp.txt 
this is Testing of date : 2016-11-25 07:20:10 

它打印變量,但在GSUB功能不工作 -

awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{print srch,repl}' temp.txt 
2016-11-25 07:20:10 [25/Nov/16:07:20:10] 

我試圖awk命令如下 -

awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{gsub("srch","repl",$0)}' temp.txt 
awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{ gsub(srch,repl,$0)}' temp.txt 
awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{ gsub("$srch","$repl",$0)}' temp.txt 
awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{ gsub(srch,repl,$0)}' temp.txt 
awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{print gsub(srch,repl,$0)}' temp.txt 
awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{ gsub("srch","repl",$0)}' temp.txt 
awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{ gsub("$srch","$repl",$0)}' temp.txt 
awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{ gsub("$srch","$repl",$0)}' temp.txt 
awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{ gsub(srch,repl,$0)}' temp.txt 

#var1="2016-11-25 07:20:10" 
#var2="[25/Nov/16:07:20:10]" 
#echo $var1 $var4 
2011-11-25 07:20:10 [25/Nov/11:07:20:10] 
#awk -v srch="$var1" -v repl="$var4" '{ gsub(srch,repl,$0)}' temp.txt 

隨着sed命令 -

#echo $var1 $var4 
2011-11-25 07:20:10 [25/Nov/11:07:20:10] 
sed 's/"$var1"/"$var4"/' temp.txt 
sed 's/$var1/'"${var4}"'/' temp.txt 
sed 's|$var1|'"${var4}"'|' temp.txt 
sed 's/\$var1/${var4}/' temp.txt 
sed 's/\$var1/$var4/' temp.txt 
sed "s/"$var1"/"$var4"/" temp.txt 
sed 's/'$var1'/'$var4'/' temp.txt 
sed 's/'$var1'/'$var4\/' temp.txt 
sed -e "s/${var1}/${var4}/' temp.txt 
sed -e "s/${var1}/${var4}/" temp.txt 
sed "s/$var1/$var4/" temp.txt 
sed 's/'"$var1"'/'"$var4"'/' temp.txt 
sed 's/'"$var1"'/'$var4'/' temp.txt 

不知道我錯過了什麼。

預期輸出 -

#this is Testing of date : [25/Nov/11:07:20:10] 
+1

至少你試過了:) –

回答

1

這將是:

var1="2016-11-25 07:20:10" 
var2="[25/Nov/16:07:20:10]" 
awk -v srch="$var1" -v repl="$var2" '{gsub(srch,repl)} 1' temp.txt 
sed 's#'"$var1"'#'"$var2"'#g' temp.txt 

您還應該閱讀Is it possible to escape regex metacharacters reliably with sed,因爲如果var1包含RE元字符或var2包含的反向引用,上述操作將失敗。

2

這一個工程,但你需要添加print或沒有將被打印:

awk -v srch="2016-11-25 07:20:10" -v repl="[25/Nov/16:07:20:10]" '{ gsub(srch,repl,$0); print}' temp.txt 

結果:

this is Testing of date : [25/Nov/16:07:20:10] 

sed可以也可以這樣做(只要你使用雙引號,或者變量不會被評估) ,但由於替換字符串包含一個斜槓,你必須改變SED表達分隔符或你得到一個sed解析錯誤,我選擇#

$ var1="2016-11-25 07:20:10"; var4="[25/Nov/11:07:20:10]" 
$ sed "s#$var1#$var4#" temp.txt 

結果:

this is Testing of date : [25/Nov/11:07:20:10] 
0

在一點切線 - 我會解決它在perl和解析你的日期。 (我知道你並沒有問perl,但我認爲這是一個非常適合這一點 - 它安裝在你最有sedawk可用的地方)

它的工作有點像這樣:

#!/usr/bin/env perl 
use strict; 
use warnings; 
use Time::Piece; 

#iterate __DATA__ special filehandle 
while (<DATA>) { 
    #extract time via regex 
    my ($time_str) = m/([\d\-]+\s+[\d\:]+)$/; 
    next unless $time_str; 
    #parse it into a Time::Piece object 
    my $timestamp = Time::Piece -> strptime ("$time_str", "%Y-%m-%d %H:%M:%S");  
    #print it using a new format string 
    my $new_time_str = $timestamp -> strftime("%d/%b/%y:%H:%M:%S"); 
    #replace old with new in line 
    s/$time_str/\[$new_time_str\]/; 
    #print current line 
    print; 
} 

__DATA__ 
this is Testing of date : 2016-11-25 07:20:10 

而對於一個命令行的解決方案的緣故,凝結成:

perl -pe '($ts)=m/([\d\-]+\s+[\d\:]+)$/;s{$ts}{Time::Piece->strptime($ts,"%Y-%m-%d %H:%M:%S")->strftime("[%d/%b/%y:%H:%M:%S]")}e;' 

可以通過管道到這一點,或指定一個文件名作爲參數,就像你用sedawk