将数据从一个源迁移到另一个源的脚本

时间:2010-11-24 20:37:04

标签: bash perl sed scripting

我有一个.h文件,其中包含这种格式的数据

struct X[]{
{"Field", "value1 value2 value"},
{"Field2", "value11 value12 value232"},
{"Field3", "x  y z"},
{"Field4", "a bbb s"},
{"Field5", "sfsd sdfdsf sdfs"};
/****************/
};

我的文本文件包含我要在.h文件中替换的值,其值为新值

value1   Valuesdfdsf1  
value2   Value1dfsdf  
value3   Value1_another  
sfsd     sfsd_ewew   
sdfdsf   sdfdsf_ew 
sdfs     sfsd_new   

生成的.h文件将包含上述文本文件中的替换项。其他一切都是一样的。

struct X[]{
    {"Field1", "value11 value12 value232"},
    {"Field2", "value11 value12 value232"},
    {"Field3", "x  y z"},
    {"Field4", "a bbb s"},
    {"Field5", "sfsd_ewew sdfdsf_ew sdfs_new"};
    /****************/
    };

请帮助我找到使用unix工具完成它的解决方案:awk,perl,bash,sed等

5 个答案:

答案 0 :(得分:3)

cat junk/n2.txt | perl -e '{use File::Slurp; my @r = File::Slurp::read_file("junk/n.txt"); my %r = map {chomp; (split(/\s+/,$_))[0,1]} @r; while (<>) { unless (/^\s*{"/) {print $_; next;}; my ($pre,$values,$post) = ($_ =~ /^(\s*{"[^"]+", ")([^"]+)(".*)$/); my @new_values = map { exists $r{$_} ? $r{$_}:$_ } split(/\s+/,$values); print $pre . join(" ",@new_values) . $post . "\n"; }}'     

结果:

struct X[]{
{"Field", "value1 Value1dfsdf value"},
{"Field2", "value11 value12 value232"},
{"Field3", "x y z"},
{"Field4", "a bbb s"},
{"Field5", "sfsd_ewew sdfdsf_ew sfsd_new"};
/****************/
};

代码解开:

use File::Slurp;
my @replacements = File::Slurp::read_file("junk/n.txt"); 
my %r = map {chomp; (split(/\s+/,$_))[0,1]} @replacements; 
while (<>) {
    unless (/^\s*{"/) {print $_; next;}
    my ($pre,$values,$post) = ($_ =~ /^(\s*{"[^"]+", ")([^"]+)(".*)$/); 
    my @new_values = map { exists $r{$_} ? $r{$_} : $_ } split(/\s+/, $values);
    print $pre . join(" ",@new_values) . $post . "\n";
}

答案 1 :(得分:2)

#!/usr/bin/perl

use strict; use warnings;

# you need to populate %lookup from the text file
my %lookup = qw(
    value1   Valuesdfdsf1
    value2   Value1dfsdf
    value3   Value1_another
    sfsd     sfsd_ewew
    sdfdsf   sdfdsf_ew
    sdfs     sfsd_new
);

while ( my $line = <DATA> ) {
    if ( $line =~ /^struct \w+\Q[]/ ) {
        print $line;
        process_struct(\*DATA, \%lookup);
    }
    else {
        print $line;
    }
}

sub process_struct {
    my ($fh, $lookup) = @_;

    while (my $line = <$fh> ) {
        unless ( $line =~ /^{"(\w+)", "([^"]+)"}([,;])\s+/ ) {
            print $line;
            return;
        }
        my ($f, $v, $p) = ($1, $2, $3);
        $v =~ s/(\w+)/exists $lookup->{$1} ? $lookup->{$1} : $1/eg;
        printf qq|{"%s", "%s"}%s\n|, $f, $v, $p;
    }
    return;
}

__DATA__
struct X[]{
{"Field", "value1 value2 value"},
{"Field2", "value11 value12 value232"},
{"Field3", "x  y z"},
{"Field4", "a bbb s"},
{"Field5", "sfsd sdfdsf sdfs"};
/****************/
};

答案 2 :(得分:1)

这是一个简单的程序:

use strict;
use warnings;
use File::Copy;

use constant {
    OLD_HEADER_FILE   => "headerfile.h",
    NEW_HEADER_FILE   => "newheaderfile.h",
    DATA_TEXT_FILE    => "data.txt",
};

open (HEADER, "<", OLD_HEADER_FILE) or
die qq(Can't open file old header file ") . OLD_HEADER_FILE . qq(" for reading);

open (NEWHEADER, ">", NEW_HEADER_FILE) or
die qq(Can't open file new header file ") . NEW_HEADER_FILE . qq(" for writing);

open (DATA, "<", DATA_TEXT_FILE) or
die qq(Can't open file data file ") . DATA_TEXT_FILE . qq(" for reading); 

#
# Put Replacement Data in a Hash
#

my %dataHash;
while (my $line = <DATA>) {
    chomp($line);
    my ($key, $value) = split (/\s+/, $line);
    $dataHash{$key} = $value if ($key and $value);
}
close (DATA);

#
# NOW PARSE THOUGH HEADER
# 

while (my $line = <HEADER>) {
    chomp($line);
    if ($line =~ /^\s*\{"Field/) {
        foreach my $key (keys(%dataHash)) {
            $line =~ s/\b$key\b/$dataHash{$key}/g;
        }
    }
    print NEWHEADER "$line\n";
}

close (HEADER);
close (NEWHEADER);
copy(NEW_HEADER_FILE, OLD_HEADER_FILE) or
  die qq(Unable to replace ") . OLD_HEADER_FILE . qq(" with ") . NEW_HEADER_FILE . qq(");

我可以使用map使其更加效率,但这会让人更难理解。

基本上:

  • 我打开三个文件,原始标题,我正在构建的新标题和数据文件
  • 我首先将我的数据放入哈希,其中替换文本由原始文本键入。 (如果我愿意,可以反过来做。
  • 然后我浏览原始标题的每一行。 **如果我看到一条看起来像是场线的线,我知道我可能需要更换。 **对于%dataHash中的每个条目,我使用$key替换值替换$dataHash{$key}。我使用\b标记字边界。这样,field11不会被替换,因为我在该字符串中看到field1。 **现在我将该行写回我的新头文件。如果我没有更换任何东西,我只需回写原始行。
  • 完成后,我将新标题复制到旧标题文件中。

答案 3 :(得分:0)

如果您的文件很大,这可能会有点慢。

gawk -F '[ \t]*|"' 'FNR == NR {repl[$1]=$2;next}{for (f=1;f<=NF;++f) for (r in repl) if ($f == r) $f=repl[r]; print} ' keyfile file.h

答案 4 :(得分:0)

这个脚本应该工作
keyval 是包含键值对的文件 filetoreplace 是包含要修改的数据的文件
名为 已更改 的文件将包含更改

#!/bin/sh
echo  

keylist=`cat keyval | awk '{ print $1}'`  


while read line   
do   

for i in $keylist  
do  


if echo $line | grep -wq $i; then  

    value=`grep -w $i keyval | awk '{print $2}'`  
    line=`echo $line | sed -e "s/$i/$value/g"`  
fi  

done  

echo $line >> changed  

done < filetoreplace