如何使用变量来搜索和替换使用sed或awk?

时间:2016-11-15 17:01:22

标签: unix awk sed

我试图在sed和awk的帮助下使用变量来查找和替换值,但没有得到任何输出 -

 class Home extends React.Component {
  render() {
    return (      
     <View style={{flex: 1}}>
       <View style={styles.header}>
         <Text style={styles.headerText}>AdView</Text>
       </View>
      <BoxLists author="station one" navigator={this.props.navigator} />
     </View>            
   );
  }
 }

它正在打印变量但在gsub函数中不起作用 -

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

我试过下面的awk命令 -

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]

使用sed命令 -

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

不确定我错过了什么。

预期产出 -

#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

3 个答案:

答案 0 :(得分: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]

答案 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

如果var1包含RE元字符或var2包含反向引用,则还应阅读Is it possible to escape regex metacharacters reliably with sed,因为上述情况会失败。

答案 2 :(得分:0)

有点切线 - 我会在perl解决它并解析你的约会。 (我知道您没有询问perl,但我认为它非常适合这种情况 - 它安装在大多数您拥有sed或{awk的地方{1}}可用)

它会有点像这样:

#!/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一样。