0

I have 2 files, 1st file it has the output and another one template. I want to add ID's in the template from output without hardcode the value.

 Output.txt,
      abc  8392382222
      def  9283923829
      ghi  2392832930

Template file,
    Pool : 1
      Some text
      Some text
      Some text
      name:
      no:
      London
      UK
      name:
      no:
      London
      UK
      name:
      no:
      London
      UK

    Pool : 2
      Some text
      Some text
      Some text
      name:
      no:
      London
      UK
      name:
      no:
      London
      UK
      name:
      no:
      London
      UK

I want to add output lines in template like below

    Pool : 1
      Some text
      Some text
      Some text
      name: abc
      no: 8392382222
      London
      UK
      name: def
      no: 9283923829
      London
      UK
      name: ghi
      no: 2392832930
      London
      UK

    Pool : 2
      Some text
      Some text
      Some text
      name: abc
      no: 8392382222
      London
      UK
      name: def
      no: 9283923829
      London
      UK
      name: ghi
      no: 2392832930
      London
      UK

2 Answers 2

1

Using just bash, you can read two files simultaneously: just redirect on of it to a different file descriptor.

#!/bin/bash
while read -r -a values ; do
    for i in {0..3} ; do
        read -u 3 line
        echo "$line ${values[$i]}" 
    done
done < output.txt 3<template.txt

Note that usually, the template file contains just the four lines that should repeat, so you can read it into an array before processing the file containing values, so you don't need any additional descriptors:

#!/bin/bash
template=()
while read -r line ; do
    template+=("$line")
done < template.txt

while read -r -a values ; do
    for (( i=0; i<${#template[@]}; ++i )) ; do
        echo "${template[$i]} ${values[$i]}" 
    done
done < output.txt
6
  • I tried both the scripts, It is displaying the output but it is not added into template file.
    – Beginner
    Mar 2 at 17:40
  • @Beginner: Just redirect the output to a temporary file, then mv the temp file to the template.
    – choroba
    Mar 3 at 9:20
  • but still I need to do lots of things. Could you please help me. can we have a call
    – Beginner
    Mar 3 at 17:29
  • I'm too busy, sorry. This site is great if you want to learn, if you just need a solution, hire someone to help you.
    – choroba
    Mar 3 at 20:23
  • Could you please look now
    – Beginner
    Mar 6 at 14:12
0

With perl:

perl -pe '
  unless (s{^\h*name:\K\h*$}{($name,$no) = split " ", <STDIN>; " $name"}e) {
    s{^\h*no:\K\h*$}{ $no}
  }' template < Output.txt

Add the -i option to edit the template file in place instead of producing the result on stdout.

That preserves the horizontal spacing before the no: and name: if any and replaces the spacing after with a single space and the values retrieved from Output.txt (opened on STDIN).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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