Replace String in a File with Value from Another File

replace string in a file with value from another file

awk 'NR==FNR{a[$2]=$1;next}{$1=a[$1];}1' fileA fileB

NR==FNR{a[$2]=$1;next} => This is true when the fileA is processed. An associative array is formed where the index is the 2nd column with the 1st column as its value.

{$1=a[$1];} => When the second file is processed, Replace the 1st column with the appropriate value stored in the array.

1 => Print every line.

Replace a string in one file with another string in another file

You can read the first file into a dictionary, and then use re.sub:

import re
d = dict(re.split('\s+', i.strip('\n')) for i in open('filename.txt'))
content = [i.strip('\n') for i in open('filename2.txt')]
with open('results.txt', 'w') as f:
f.write('\n'.join(re.sub('(?<=\<\!\-\-\s)\d+(?=\s\-\-\>)', lambda x:d[x.group()], i) for i in content))

Output:

...text... <!-- name44 --> ...text...
...text... <!-- name2 --> ...text...
...text... <!-- name3 --> ...text...
...text... <!-- name94 --> ...text...

Replace a string in a file with contents copied from another file

If I have properly understood your question, the following should do the trick:

#!/bin/bash

fileToBeRead="key.txt" #Whatever

var=$(tr -d '[:blank:]\n' < $fileToBeRead)
sed -i "s#sdkPrivateKey=\"\"#sdkUniqueKey=\"$var\"#g" AppConstants.txt

Since the key contains backslashes/slashes you should use a different separator for sed (e.g. #) or the sentence will be misparsed.

EDIT:

KEY="$var" perl -pi -e 's/sdkPrivateKey=""/sdkUniqueKey="$ENV{KEY}"/g' AppConstants.txt

perl can be used instead of sed in order to avoid sed's separator issues. Check out @DennisWilliamson's comment below.

Replace value in file with value from another file based on matching string (otherwise: skip)

Following awk may help you on same.

awk 'FNR==NR{a[$1]=$2;next} {print $1,$1 in a?a[$1]:$2}' OFS="\t"  Input_file2   Input_file1

Add > file3.txt to above code to get the output into output filw named file3.txt too.

Find string in file and replace with value from another file for multilines - powershell

$fileA = Get-Content -Path fileA.txt
$fileB = Get-Content -Path fileB.txt

$result = @()
foreach ($lineA in $fileA) {
$linkExistsInFileB = $false
foreach ($lineB in $fileB) {
if ($lineB -ilike "$lineA*") {
$linkExistsInFileB = $true
$result += $lineB
break
}
}
if (-not $linkExistsInFileB) {
$result += "$lineA|LINK NOT EXIST"
}
}
$result

Output:

L5020|http://linktosite.de|URL|P555
L100|http://sitelink.de|URL|P523
L50|http://abcde.de|URL|P53
L511|http://bbcccddeee.de|URL|P540
L300|http://link123456.de|URL|LINK NOT EXIST
L5450|http://randomlink.de|URL_DE|LINK NOT EXIST
L5460|http://randomwebsitelink.de|URL_DE|LINK NOT EXIST

replace strings in column with matching value from another file using awk

You didn't reference the associative array in the right way. Please change:

...{if ($3 in a){$3=a[1]}; print $0...

into:

...{if ($3 in a){$3=a[$3]}; print $0

The keys of your array a are AAA,AAB... instead of 1,2,3....

Replace string in a file with a specific field value from another file with awk

GNU awk solution:

awk 'NR==FNR{ 
if (NR==1) next;
c=0; f[$1][++c]=$2; f[$1][++c]=$3; next
}
{
c=0;
for (i in f) {
b[++c]=$0;
gsub(/STRING_1/, f[i][1], b[c]);
gsub(/STRING_2/, f[i][2], b[c]);
print b[c] > i
}
}' file2 file1
  • f[$1][++c] - multidimensional array f where $1 is a parent key (for ex. out1) and ++c points to ordinal field number (i.e. 1 and 2)
  • for (i in f) - iterating through output filenames

Viewing results:

$ head out[12]
==> out1 <==
bla bla bla ABCDEF blabla GHIJKL.
bla bla bla bla bla.

==> out2 <==
bla bla bla MNOPQR blabla STUVWX.
bla bla bla bla bla.

How to read a file and replace a string in another file using shell script in linux?

#!/bin/sh
source "/opt/source/source.txt"
USERN="username: '$USERNAME',"
PWD="password: '$PASSWORD',"

echo $USERN
echo $PWD

sed -i "s/username:.*/$USERN/" target.js
sed -i "s/password:.*/$PWD/" target.js

There were a few issues:

  • USER is a reserved shell variable so you have to give it a different name
  • your sed script needs s/.../.../ to substitute and you also need to match the rest of the line in your regexp so the .* is needed or the old credentials are still in the output
  • -i option on sed is needed for an in-place edit
  • variable substitution is not performed in a string delimited with ', so changed to " here


Related Topics



Leave a reply



Submit