Thursday, 24 September 2020

Shell SED&&AWK find content between tags, BashVariables, sample combine xml script

     awk '/\<tag1\>/{flag=1; next} /\<\/tag1closure\>/{flag=0} flag' "$file" >> "filename"

https://stackoverflow.com/questions/38972736/how-to-print-lines-between-two-patterns-inclusive-or-exclusive-in-sed-awk-or

https://stackoverflow.com/questions/17988756/how-to-select-lines-between-two-marker-patterns-which-may-occur-multiple-times-w



Bash TimeStamp

 date +"%T" # current time

https://stackoverflow.com/questions/17066250/create-timestamp-variable-in-bash-script


Bash Variable assignment must have no space && smple combine XML script

Joey sample script 1

#!/bin/bash

timeStamp=$(date +%s)

fileOutPutName="allInOneSample_$timeStamp.xml"

dirPath="data"


echo '<?xml version="1.0" encoding="UTF-8"?>' >> "$fileOutPutName"

echo '<unload unload_date="2020-09-24 18:45:15">' >> "$fileOutPutName"

for file in "$dirPath"/*; do

    if [ ! -d "$file" ]; then

      awk '/\<unload\>/{flag=1; next} /\<\/unload\>/{flag=0} flag' "$file" >> "$fileOutPutName"

    fi

done


Joey sample script 2

#!/bin/bash


timeStamp=$(date +%s)

unloadDate=$(date '+%Y-%m-%d  %H:%M:%S')

fileOutPutName="myfile_name.xml"

dataSourceFolderPath="data"


echo '<?xml version="1.0" encoding="UTF-8"?>' >> "$fileOutPutName"

echo '<unload unload_date="'$unloadDate'">' >> "$fileOutPutName"

for file in "$dataSourceFolderPath"/*; do

    if [ ! -d "$file" ]; then

      awk '/\<unload\>/{flag=1; next} /\<\/unload\>/{flag=0} flag' "$file" >> "$fileOutPutName"

    fi

done


echo ' </unload>' >> "$fileOutPutName"


echo ' </unload>' >> "$fileOutPutName"

No comments:

Post a Comment