Assuming your input is stored in a file input_file.xml, to extract the values between the <ColumnHeader> and </ColumnHeader> tags in a xml/txt file you can run a shell command something like this:
grep -oP '<ColumnHeader>\K.*?(?=<\/ColumnHeader>)' input_file.xml > extracted_text.txt
To store the extracted value in a variable in the same script to be reused later:
extracted_text=$(grep -oP '<ColumnHeader>\K.*?(?=<\/ColumnHeader>)' input_file.xml)
If you need to replace the original file with some new text and create a new file (which I think is your use case):
sed -E 's/<ColumnHeader>.*<\/ColumnHeader>/REPLACEMENT_TEXT/' input_file.xml > replaced_file.xml
In the above command of course you can use a variable insted of REPLACEMENT_TEXT:
sed -E 's/<ColumnHeader>.*<\/ColumnHeader>/$replacement_text/' input_file.xml > replaced_file.xml
If you need to replace the text in the original file then do this:
sed -E -i 's/<ColumnHeader>.*<\/ColumnHeader>/REPLACEMENT_TEXT/' input_file.xml
Hope this helps.
Edit:
After going through the updated question, here's a new approach that might be suitable in this scenario:
You can achieve this by using 'awk', a powerful text processing tool
@kikin - Please try below command for a sample input.xml file which contains your input xml content.
awk -F'[<>]' '/<ColumnHeader>/ && !c { c++; $3="new text 1" } /<ColumnHeader>/ && c { c++; $3="new text 2" } 1' input.xml > output.xml
Note: In this command we can use different parameters instead of hard coded strings 'new text 1' and 'new text 2':
str1="new text 1"
str2="new text 2"
awk -F'[<>]' '/<ColumnHeader>/ && !c { c++; $3="$str1" } /<ColumnHeader>/ && c { c++; $3="$str2" } 1' input.xml > output.xml
Also if this works for your core requirement of replacing the texts, then please use a while loop to loop over a txt file (e.g.: file_names.txt) that contains the list of all the files which you want to make the changes to as below:
while IFS= read -r file_name; do
awk -v RS='</?ColumnHeader>' -v ORS= 'NR%2 { print $0; next } { gsub(/Active/, "new text 1"); gsub(/Default/, "new text 2"); print $0 }' "$file_name" > "${file_name%.xml}_output.xml"
done < file_names.txt
file_names.txt file sample content:
Note: This file list can be populated from a
input_file1.xml
input_file2.xml
input_file3.xml