1
sed '/Var4/R replace.txt' input.txt | sed '/Var4/{N;s/"Var4"\n\(.*\)/"\1"/}' > "output.txt"

Works perfectly if "Var4" is at the end of the line, however if any text exists after it, the output is incorrect ?

Input.txt

TextBox1.Value = "Var4"  Then
Else
TextBox1.Value = "- - - - - - -"
End If

TextBox1.Value = "Var4"  Then
Else
TextBox1.Value = "- - - - - - -"
End If

Output.txt should look like this:

TextBox1.Value = "0000AAAA"  Then
Else
TextBox1.Value = "- - - - - - -"
End If

TextBox1.Value = "0000BBBB"  Then
Else
TextBox1.Value = "- - - - - - -"
End If

Replace.txt

0000AAAA
0000BBBB
0000CCCC

Output.txt after using:

sed '/Var4/R replace.txt' input.txt | sed '/Var4/{N;s/"Var4"\n\(.*\)/"\1"/}' > "output.txt"

TextBox1.Value = "Var4"  Then
0000AAAA
Else
TextBox1.Value = "- - - - - - -"
End If

TextBox1.Value = "Var4"  Then
0000BBBB
Else
TextBox1.Value = "- - - - - - -"
End If

Any Advice ?

1
  • Sorry Not working >>>>>> sed '/Var4.*/R replace.txt' input.txt | sed '/Var4/{N;s/"Var4"\n(.*)/"\1"/}' > "output.txt"
    – Xlance
    Commented Jul 8, 2018 at 23:07

1 Answer 1

2

The /R command of GNU sed can't be used here because there is no chance to use the values from replace.txt for processing. /R will write them to stdout immediately, not into the pattern space. That means that there is no access from any sed command to the content of replace.txt.

Described here for example: http://www.grymoire.com/Unix/Sed.html#uh-37

the "r" [or R] command writes the file to the output stream. The file is not inserted into the pattern space, and therefore cannot be modified by any command.


Solution:

I would use awk. Like this:

awk 'NR==FNR{r[NR]=$0;next}/"Var4"/{sub(/Var4/,r[++i])}1' replace.txt input.txt

Explanation using a multiline version:

# True as long as we are reading the first file (replace.txt)
NR==FNR{
    r[NR]=$0 # Store replacement in an array r
    next     # Don't enter the blocks below
}

# When "Var4" is found
/"Var4"/{
    # replace Var4 by next element of r
    sub(/Var4/,r[++i])
}

1 # Print the line

Btw, even if it would work, the /R command is a GNU extension of sed and will not work in other versions of sed. The above awk script should work with any version of awk.

6
  • Perfect ! Thank you - What is the difference between sed & awk ?
    – Xlance
    Commented Jul 8, 2018 at 22:59
  • 2
    Check this: stackoverflow.com/questions/1632113/…
    – hek2mgl
    Commented Jul 8, 2018 at 23:00
  • With a little Tweak, this also works >>>>> sed '/Var4/R replace.txt' input.txt | sed '/Var4/{N;s/"Var4"(.*)\n(.*)/"\2"\1/}' > "output.txt"
    – Xlance
    Commented Jul 9, 2018 at 8:48
  • yes, that should work but that would process in the input file twice which is not really efficient.
    – hek2mgl
    Commented Jul 9, 2018 at 9:57
  • 2
    /"Var4"/{ sub(/Var4/,r[++i]) } will work with the OPs posted sample input data but not with other data (e.g. if a & appears in replace.txt or if Var4 was Var.4). Personally I'd use BEGIN{old="\"Var4\""; lgth=length(old)-2} ... s = index($0,"\"Var4\"") { $0 = substr($0,1,s) r[++i] substr($0,s+lgth) } so the comparison and replacement are both literal string operations and so will work given any input.
    – Ed Morton
    Commented Jul 9, 2018 at 11:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.