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 ?

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

1 Answers1

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.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Perfect ! Thank you - What is the difference between sed & awk ? – Xlance Jul 08 '18 at 22:59
  • 2
    Check this: https://stackoverflow.com/questions/1632113/what-is-the-difference-between-sed-and-awk – hek2mgl Jul 08 '18 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 Jul 09 '18 at 08:48
  • yes, that should work but that would process in the input file twice which is not really efficient. – hek2mgl Jul 09 '18 at 09: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 Jul 09 '18 at 11:45
  • Good point. I wanted to keep the script simple here on SO. A production solution should also include a check for the length of replace.txt. I mean it should fail when there are no more indexes in `r` – hek2mgl Jul 09 '18 at 15:52