Skip to main content
added 236 characters in body
Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

Complex Grep + GNU Awk solution:

The crucial awk script content, let's say add_param_by_id.awk:

#!/bin/awk -f BEGIN{ FS=":[[:space:]]+" } NR==FNR{ a[$1]=$2;a[$1] = $2; next } match($0, /\<id="([^"]+)"/, b) && b[1] in a{ sub(/\<id="[^"]+"/, "& some_param=\042"a[b[1]]"\042") }1 

The main commands:

export pat="($(cut -d':' -f1 source.yml | paste -s -d'|'))" grep -ElZr id=\"$pat\""\<id=\"$pat\"" --include=*.txt | xargs -0 -I{} sh -c \ 'awk -f add_param_by_id.awk source.yml "$1" > "/tmp/$$" && mv "/tmp/$$" "$1"' _ {} 

  • pat - variable containing regex alternation group with identifiers i.e. (apples|grapes|trees|strawberry)
  • grep -ElZr id=\"$pat\" --include=*.txt - prints all filenames which were matched for any of the specified identifiers

Example file content after processing:

This is some plain text Toreplace string contains id="apples" some_param="Apples are very tasty" The most vitamin-rich berry is id="strawberry" some_param="Yummy" 

Complex Grep + GNU Awk solution:

The crucial awk script content, let's say add_param_by_id.awk:

#!/bin/awk -f BEGIN{ FS=":[[:space:]]+" } NR==FNR{ a[$1]=$2; next } match($0,/\<id="([^"]+)"/,b) && b[1] in a{ sub(/\<id="[^"]+"/,"& some_param=\042"a[b[1]]"\042") }1 

The main commands:

export pat="($(cut -d':' -f1 source.yml | paste -s -d'|'))" grep -ElZr id=\"$pat\" --include=*.txt | xargs -0 -I{} sh -c \ 'awk -f add_param_by_id.awk source.yml "$1" > "/tmp/$$" && mv "/tmp/$$" "$1"' _ {} 

  • pat - variable containing regex alternation group with identifiers i.e. (apples|grapes|trees|strawberry)
  • grep -ElZr id=\"$pat\" --include=*.txt - prints all filenames which were matched for any of the specified identifiers

Complex Grep + GNU Awk solution:

The crucial awk script content, let's say add_param_by_id.awk:

#!/bin/awk -f BEGIN{ FS=":[[:space:]]+" } NR==FNR{ a[$1] = $2; next } match($0, /\<id="([^"]+)"/, b) && b[1] in a{ sub(/\<id="[^"]+"/, "& some_param=\042"a[b[1]]"\042") }1 

The main commands:

export pat="($(cut -d':' -f1 source.yml | paste -s -d'|'))" grep -ElZr "\<id=\"$pat\"" --include=*.txt | xargs -0 -I{} sh -c \ 'awk -f add_param_by_id.awk source.yml "$1" > "/tmp/$$" && mv "/tmp/$$" "$1"' _ {} 

  • pat - variable containing regex alternation group with identifiers i.e. (apples|grapes|trees|strawberry)
  • grep -ElZr id=\"$pat\" --include=*.txt - prints all filenames which were matched for any of the specified identifiers

Example file content after processing:

This is some plain text Toreplace string contains id="apples" some_param="Apples are very tasty" The most vitamin-rich berry is id="strawberry" some_param="Yummy" 
Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

Complex Grep + GNU Awk solution:

The crucial awk script content, let's say add_param_by_id.awk:

#!/bin/awk -f BEGIN{ FS=":[[:space:]]+" } NR==FNR{ a[$1]=$2; next } match($0,/\<id="([^"]+)"/,b) && b[1] in a{ sub(/\<id="[^"]+"/,"& some_param=\042"a[b[1]]"\042") }1 

The main commands:

export pat="($(cut -d':' -f1 source.yml | paste -s -d'|'))" grep -ElZr id=\"$pat\" --include=*.txt | xargs -0 -I{} sh -c \ 'awk -f add_param_by_id.awk source.yml "$1" > "/tmp/$$" && mv "/tmp/$$" "$1"' _ {} 

  • pat - variable containing regex alternation group with identifiers i.e. (apples|grapes|trees|strawberry)
  • grep -ElZr id=\"$pat\" --include=*.txt - prints all filenames which were matched for any of the specified identifiers