Grep with Special Characters

I need to sort a file, the sort is not a alphabetical sort, it's based on a predefined order which is read from a file called fSortOrder.

The format of the fSortOrder file is :
STARTPATH"
....
....

The file that needs to be sorted is called tmpUnsorted and contains data in the format :
<add key="STARTPATH" value="\\njros1a2226\LIFEPRO\V13\START" />
....
....

I read the fSortOrder file one line at a time and place this in a variable called lineMO. I use the line below to search the input for the variable :

isThere="$(grep $lineMO tmpUnsorted)"

This appears to work fine, but messes up the data, instead of placing
<add key="STARTPATH" value="\\njros1a2226\LIFEPRO\V13\START" /> in the variable.

<add key="STARTPATH" value="\njros1a2226\LIFEPRO\V13\START" /> is placed in the varialbe.

Is there anyway to fix this as the double \\ is vital and the file is useless without it.

Try to replace

isThere="$(grep $lineMO tmpUnsorted)"

by

isThere="$(grep '$lineMO' tmpUnsorted)"

or

isThere=$(grep "$lineMO" tmpUnsorted)

Using "$lineMo" does not work (tried it)

Using '$lineMO' also does not work because the ' ' negates the meaing of $ and searches for the string $lineMO in the file rather than the contents of the variable $lineMO

I beleive your code is already working. Try using:

isThere="$(grep $lineMO tmpUnsorted)"

and then check the varaible with:

print -r "$lineMO"

The -r will ignore special characters and you should see that the varaible does contain \\

print -r $lineMO works fine, but for some reason it's putting spaces in the variable before the first characrer

Instead of looking like this :
<add key="STARTPATH" value="\\njros1a2226\LIFEPRO\V13\START" />

It looks like this
<add key=".........STARTPATH" value="\\njros1a2226\LIFEPRO\V13\START" />
*The dots represent the blank spaces that appear.

How do you remove a leading tab from a variable?

The grep command is only going to return what it finds in tmpUnsorted so if there is not a tab in tmpUnsorted I would check your code to see where you have embeded it.

Removing a leading tab(specifally for your code):

TABLESS=$(print -r $isThere | sed 's/=" */="/') # Removes multiple whitspaces after ="

TAB=$(printf "\t")
TABLESS=$(print -r $isThere | sed "s/=\"$TAB/=\"/") # Removes single tab character after ="

Cheers for the help guys, that's that sorted now. :b: