Script to transfer data from one source to another

I have a .h file, among other things, containing data in this format

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

I have a text file containing the values โ€‹โ€‹that I want to replace in the .h file with the new values

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

And the resulting .h file will contain the replacements from the text file above. Everything else remains the same.

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"};
    /****************/
    };

Please help me come up with a solution to execute it using the unix tools: awk, perl, bash, sed, etc.

+3
source share
5 answers

script 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
0
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";
}
+3
#!/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

:

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 . ** . , .
  • , .
+1

, .

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
0

Source: https://habr.com/ru/post/1776779/


All Articles