1822002 Members
4089 Online
109639 Solutions
New Discussion юеВ

split on \ in awk

 
SOLVED
Go to solution
Don Spare
Regular Advisor

split on \ in awk

I am trying to build a script that differentiates between Unix and Windows file name syntax - the main difference being Unix directories are separted with "/" and Windows with "\". In particular, I am trying to extract the Oracle version from a file specification on the assumption that the last group of characters after the "/" or "\" is the version number that I want. In the example below I can get a proper response from the Unix side but the Windows side always returns NULL which I am taking to mean that the split is not finding the proper delimiter. Any clues?
Is there a special way to refer to \ within awk so it can be used as a field delimiter?

if [ "`grep $ORACLE_SID $MAP_DIR/system_orabase_nt`" = "" ]; then
ORACLE_VERSION=`grep -i "$HOSTNAME $ORACLE_SID " $MAP_DIR/system_orabase | awk '{split($4,a,"/"); print a[4] }'`
echo "Found in system_orabase"
else
ORACLE_VERSION=`grep -i "$REAL_HOSTNAME $ORACLE_SID " $MAP_DIR/system_orabase_nt | awk '{split($4,a,"\5c"); print a[4] }'`
echo "Found in system_orabase_nt"
fi
echo "ORACLE_VERSION = $ORACLE_VERSION"

The 2 data files are attached.
9 REPLIES 9
Don Spare
Regular Advisor

Re: split on \ in awk

I could only attach 1 file. Here is the 2nd.
A. Clay Stephenson
Acclaimed Contributor

Re: split on \ in awk

You simply need to 'escape' the backslash with a backslash. instead of "\5c" or "\" use "\\" to generate a SINGLE backslash.
If it ain't broke, I can fix that.
Don Spare
Regular Advisor

Re: split on \ in awk

I tried that but I still don't get the split I'm looking for. The end result is a null.
Tim D Fulford
Honored Contributor

Re: split on \ in awk

Try

awk -F"\" '{print $4}' ......file....

Tim
-
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: split on \ in awk

My awk test just worked fine. I suggest that you eliminate the awk part of the above commands and examine the output first. I think your problem must be earlier in the chain because the "\\" absolutely will work. In fact an alternate method to avoid the split is to specify the field separator again escaping the backslash:

awk -F\\ '{print $4}'

will work and is a little more obvious.

I would surrond each shell var with ${}'s e.g. ${HOSTNAME} and replace the archaic `command` with $(command) -- that becomes especially important when you need to nest commands.
If it ain't broke, I can fix that.
Leif Halvarsson_2
Honored Contributor

Re: split on \ in awk

Hi,

Perhaps not the answere of the awk question but to get the Oracle version out of the string, try the following:
basename `echo 'C:\Oracle\9.2.0' |tr "\134" /`
You can use the same command both for Unix and windows filenames. This tr statement does not change Unix filnames.


Don Spare
Regular Advisor

Re: split on \ in awk

I'm beginning to think this might have something to do with my environment or maybe I'm out of rev on patches for awk or something. I got the -F\\ switch to work when I put the first $4 into a separate variable and then echoed the variable piped through awk -F\\ '{print $3}'. (Yea $3 is correct. I had it wrong in the original example but that is not the problem.) However, when I try to pipe it all together the result is null. I even tried doing the awk in 2 steps, eg. -

awk '{print $4}' | awk -F\\ '{print $3}'

but that didn't work either.

A. Clay, I don't doubt that it should work, all I know is that it doesn't in my environment. I am running ksh vs sh. Could that be an issue?

Don
Don Spare
Regular Advisor

Re: split on \ in awk

Eureka!! After taking A. Clay's recommendation about wrapping nested commands in () instead of single back-quotes the command is now working and producing the desired results.

Can someone explain what the difference is? Since I don't get much training, I'm not up to speed on all the latest stuff (or even some of the not-so-latest stuff).

Thanks for all your input.

Don
A. Clay Stephenson
Acclaimed Contributor

Re: split on \ in awk

In the font I'm using it is very difficult to distinguish the backticks from the single quotes so I can't really say what you fixed; however, disciplined shell programmer will ALWAYS enclose shell variables in {}'s and also tend to enclose commands in ()'s rather than ``'s. I mainly suggested the change because when I have to ask what this quote is doing .... there's probably a better way.

Using the ()'s it is so much easier to find the single quotes, double quotes, and escaped quotes.
If it ain't broke, I can fix that.