Anks
Anks

Reputation: 3

Copy a template over multiple times in a single file with an incremental sequence and name field copy from other file

I have a placeholder rule, I need to copy it with an increment sequence according to "country" name which further needs to be copied from another file in a single file.

# Cat country
Afghanistan
Albania
Algeria
Andorra
Angola
Antigua and Barbuda
Argentina
Armenia

This file contains 195 entries & there are no space b/w lines, each entry is in a new line. These entries are not fixed, sometime it could be 10 or 100.

    # cat rule
Rule_set S_1
{
 Rule S_1_R1
        Conditions ADDR in country
       Actions    Invoice to bill
}

** the Sequence ID of Rule_Set S_ID & Rule S_ID_R1 needs to be same.

To copy 100 times, I have used this.

  for i in {1..100}; do cat rule >> file2; done

And sequence

awk -vRS=S_1 '{$0=n$0;ORS=RT}++n' file2  > new_rule

But sequencing gives me not same ID in a single rule and I still don't know to replace the country string with the list of countries.

Expected output

Rule_set S_1
{
    Rule S_1_R1
        Conditions ADDR in Afghanistan
        Actions    Invoice to bill
}
Rule_set S_2
{
    Rule S_2_R1
        Conditions ADDR in Albania
        Actions    Invoice to bill
}
.
.
.
.

Rule_set S_195
{
    Rule S_195_R1
        Conditions ADDR in Zimbabwe
        Actions    Invoice to bill
}

Upvotes: 0

Views: 93

Answers (1)

tripleee
tripleee

Reputation: 189679

Something like this?

awk '{ print "Rule_set S_" NR "\n{\n RuleS_" NR "_R1\n    Conditions " \
       "ADDR in " $0 "\n    Actions   Invoice to bill\n}\n" }' country

Upvotes: 2

Related Questions