#!/bin/bash #Recomment that all arguments that are not stright forward be enclosed in single quotes #Example call is #$ source KeyUpDownFunction #$ KeyUpDownFunction 'root="PROCEDURE_4234_52' '' 'MyTestFile.xml' #First argument is the line number inside the section you wish to isolate. for instance for a problem, it might be a number you have collected from a file containing all of the line numbers of the problem QRDA template, 2.16.840.1.113883.3.249.11.100.8 #Second argument is the tag or some other marker of the top of the section you with to isolate such as or prehaps #Third argument is the tag or some other marker of the top of the section you with to isolate such as or prehaps #fourth argument is the filename you are doing this in #The fifth argument is optional and is the Marker you wish to leave in the file showing where this section began. It might be an empty tag or a comment - this part has not been added to the function yet LineNumberUpDownFunction() { local LineNum local Result local BeginLineNum local EndLineNum local Result2 local KeyLineNumber local Marker File="$4" echo "File is $File" KeyLineNumber="$1" echo "KeyLineNumber is $KeyLineNumber" StartElement="$2" echo "Starting Element is $StartElement" EndElement="$3" echo "EndElement is $EndElement" #Marker="$5" LineNum="$KeyLineNumber" echo "LineNum is starting at $KeyLineNumber" #Start at the line number you have chosen and move #backward one line at a time and find the line number of that has what is in the second argument #Greping for the Element LineNumb returns $? with 0 is true and 1 is false if the element is in the line #work backwards from LineNum to find the line with what you want in it Result="$(head -"$LineNum" "$File" |tail -1 | grep -q "$StartElement";echo $?)" echo "Result of last check of the line number is $Result" echo "The current line number being tested is $LineNum" echo "The next line number to test is $(($LineNum-1))" while [ "1" = "$Result" ] do LineNum="$(($LineNum-1))" echo "$LineNum" Result="$(head -"$LineNum" "$File" |tail -1 | grep -q "$StartElement";echo $?)" done BeginLineNum="$LineNum"; echo "BeginLineNum is $BeginLineNum" #initialize EndLineNum which will ultimately be the line with the ending component tag, "$EndElement" EndLineNum="$(($LineNum+1))"; echo "EndLineNum is starting at $EndLineNum" #This is to check the line and if it does not contain $EndElement, add 1 to EndLineNum and go to the next line and check that one Result2="$(head -"$EndLineNum" "$File" | tail -1| grep -q "$EndElement";echo "$?")" #Result2="$(sed -n "$EndLineNum"p "$File" | grep -q "$EndElement";echo "$?")" echo "Result2 is $Result2" while [ "$Result2" = "1" ] do EndLineNum="$(($EndLineNum+1))" echo "EndLineNum is $EndLineNum" # echo "$(head -"$EndLineNum" "$File" | tail -1)" Result2="$(head -"$EndLineNum" "$File" | tail -1 | grep -q "$EndElement";echo "$?")" echo "Result2 is $Result2 and the EndElement is $EndElement and current line being teste is $EndLineNum" done echo "Beginning line Number of the Section is $BeginLineNum" echo "Ending Line Number of the Section is $EndLineNum" #Now you know the beginning and ending line number of the section so you can delete it or process it somehow. It returns the key and name beginning line number the ending line number, the marker and the file echo " KeyLineNumber is $KeyLineNumber, the Beginning of the section has the starting element $StartElement and is on line number $BeginLineNum and the line number that EndElement which is $EndElement is $EndLineNum, the File name is $File and the place holder left in the file, if there is one, is $Marker" #The Test Section information variable is available to use in your additional processing SectionInformation="$(echo "$KeyLineNumber:$BeginLineNum:$EndLineNum:$File $Marker")" echo "end LineNumberUpDownFunction" echo "the KeyLineNumer BeginlineNum and EndLineNum File and Marker have been written to the variable SectionInformation separated by colons." echo "$KeyLineNumber:$BeginLineNum:$EndLineNum:$File $Marker" #echo "Do you wish to delete the Marker?" read }