1

I want to use the sed command (or something that works) to replace a word in a template file with a word in a line of another file.

As an example I have a file with a list of words, each word is in a different line and I want to use sed to take the first word (which is in the first line) and put it in another file where the word "value1" is written. I thought that with this post I could be able to do it, but I can't figure it out.

Graphic example:

File A:

Maria
Albert
Toni
Henry
Tom

File B:

The name of the student is: value1

Expected output for line 3:

The name of the student is: Toni

I want to be able to move one of the names from file A to file B where value1 is placed. And I want to do it multiple times.

closed as unclear what you're asking by Philippos, Christopher, G-Man, Rui F Ribeiro, njsg Apr 21 '18 at 2:12

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • post a sample input content and expected result – RomanPerekhrest Apr 18 '18 at 8:56
  • 1
    Is your template always a single line? – Stéphane Chazelas Apr 18 '18 at 9:36
  • 1
    Does value1 always occur one and only one time in the template? – Stéphane Chazelas Apr 18 '18 at 9:37
  • 1
    Please expand your example to cover several values and lines. I still don't understand why value1 is douns on line 3. – Philippos Apr 18 '18 at 11:45
  • 1
    Why not use printf formats instead of your own format? Example ' The name of the student is %s', then you would not need to do the search and replace of value1 which @Stéphane Chazelas does in his perl script. – Raphael Ahrens Apr 20 '18 at 6:36
1

I'd use perl:

perl -ne '
  BEGIN{
    local $/ = undef;
    $template = <STDIN>; # slurp file B in
  }
  chomp;
  print $template =~ s/\bvalue1\b/$_/gr' fileA < fileB

If your version of perl is too old to support the r substitute flag, you can use a temporary variable:

perl -ne '
  BEGIN{
    local $/ = undef;
    $template = <STDIN>; # slurp file B in
  }
  chomp;
  ($out = $template) =~ s/\bvalue1\b/$_/g;
  print $out' fileA < fileB
  • It gives me a compilation error and I never used perl... :/ Any suggestions? – Enrique Torelló Perelló Apr 18 '18 at 10:05
  • @EnriqueTorellóPerelló, possibly, you have an old version of perl before the r flag was added. – Stéphane Chazelas Apr 18 '18 at 10:15
  • where do I specify the line from file A to replace in file B? I suppose that value1 is the value from file B to be replaced. Right? – Enrique Torelló Perelló Apr 18 '18 at 10:41
  • @EnriqueTorellóPerelló, that answer answers your question as currently stated in the body of your question, the additional comments to your question seem to suggest a different question. I've given up trying to guess what you actually want to do. You should edit your question with a more relevant example. – Stéphane Chazelas Apr 18 '18 at 11:53

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