bad option(s) with awk

Hi All

I am trying to split a string which is like below

-p/usr/home/dfusr/adm/props/Comments.properties_-iPEL17

i want to split "_" as delimiter.

Please find the code below

#!/bin/ksh
cd /usr/home/dfusr/backup
OriginalString='-p/usr/home/dfusr/adm/props/Comments.properties_-iPEL17'
myCmdLineArg1=""
myCmdLineArg2=""

set -A cmdLineArgs `echo "$OriginalString"| awk '{z=split($0,flds,"_")
   for(i=1;i<=z;i++)
   print flds}'`
  echo "cmdLineArgs[0]:::::::: ${cmdLineArgs[0]}"
  echo "cmdLineArgs[1]:::::::: ${cmdLineArgs[1]}"
  myCmdLineArg1=${cmdLineArgs[0]}
  myCmdLineArg2=${cmdLineArgs[1]} 

I am getting bad option(s) exception

Please advice where i am doing wrong in above code. Does awk works with "-" at first place.

Please correct my code.

Thanks-
Raj

Change the following line from:

set -A cmdLineArgs `echo "$OriginalString"| awk '{z=split($0,flds,"_")

to:

set -A cmdLineArgs -- `echo "$OriginalString"| awk '{z=split($0,flds,"_")

And, of course, in this case you can use shell parameter expansion instead of external commands.

You can also do :

#!/bin/ksh -x

OriginalString='-p/usr/home/dfusr/adm/props/Comments.properties_-iPEL17'

oldIFS="$IFS" ; IFS=
set -A cmdLineArgs -- $OriginalString
IFS="$oIFS"

echo "cmdLineArgs[0]:::::::: ${cmdLineArgs[0]}"
echo "cmdLineArgs[1]:::::::: ${cmdLineArgs[1]}"

I have also tried :

IFS=_ set -A cmdLineArgs -- $OriginalString

The problem is that this command works fine at the prompt, but not from a script file. Somebody can explain that ?

Jean-Pierre

I think you mean:

IFS=_

not:

IFS= 

You should have set IFS to _ before that command.
You cannot set IFS only for the set builtin using that syntax.
You could write something like this:

zsh-4.3.10[t]% cat s
#!/bin/ksh

OriginalString='-p/usr/home/dfusr/adm/props/Comments.properties_-iPEL17'

IFS=_ ; set -A cmdLineArgs -- $OriginalString

echo "cmdLineArgs[0]:::::::: ${cmdLineArgs[0]}"
echo "cmdLineArgs[1]:::::::: ${cmdLineArgs[1]}"
zsh-4.3.10[t]% ./s  
cmdLineArgs[0]:::::::: -p/usr/home/dfusr/adm/props/Comments.properties
cmdLineArgs[1]:::::::: -iPEL17

---------- Post updated at 03:29 PM ---------- Previous update was at 03:21 PM ----------

Or you can use zsh :slight_smile:

zsh-4.3.10[t]% s=-p/usr/home/dfusr/adm/props/Comments.properties_-iPEL17
zsh-4.3.10[t]% print -- ${${(s:_:)s}[1]}                                
-p/usr/home/dfusr/adm/props/Comments.properties
zsh-4.3.10[t]% print -- ${${(s:_:)s}[2]}
-iPEL17

On my AIX box, seting IFS for the set builting work fine (at the prompt level, not in a script file) :

> OriginalString='-p/usr/home/dfusr/adm/props/Comments.properties_-iPEL17'
> set -x
> IFS=_ set -A cmdLineArgs -- $OriginalString
+ IFS=_
+ set -A cmdLineArgs -- -p/usr/home/dfusr/adm/props/Comments.properties -iPEL17
> echo "cmdLineArgs[0]:::::::: ${cmdLineArgs[0]}"
+ echo cmdLineArgs[0]:::::::: -p/usr/home/dfusr/adm/props/Comments.properties
cmdLineArgs[0]:::::::: -p/usr/home/dfusr/adm/props/Comments.properties
> echo "cmdLineArgs[1]:::::::: ${cmdLineArgs[1]}"
+ echo cmdLineArgs[1]:::::::: -iPEL17
cmdLineArgs[1]:::::::: -iPEL17
> 

Jean-Pierre.

Could you please post the output of the following command?

ksh -c '
  printf "%s" "$IFS" | od -bc
  IFS=_ set -A ar a_b
  printf "array contains: %s\n" "${ar[@]}"
  '

Thank You Its Working.

One more question.

I am reading data from the database and writing to temporary file in the below format.
1=XP|external_component|com.adp.meetingalertemail.processing.MeetingAlertEmail|EMAILALERTPUSH|32|4#XP|classpath|/usr/home/dfusr/lib/xalan.jar:
/usr/home/dfusr/lib/xerces.jar:
/usr/home/dfusr/lib/xml.jar:
/usr/home/dfusr/lib/classes12.zip

The data is writing in multiple rows as i want to write the same in one row for the same sequnce number. 1 in the above example

Find below the code i am retrieving from the database and writing to myJobTaskAttribListFile.

sqlplus -s $DBCredentials 1> $myJobTaskAttribListFile <<-EndOFSQL
SET DEFINE OFF;
SET SERVEROUT ON
SET LINESIZE 3400;
DECLARE
i_job_id NUMBER := $myJobId ;
o_run_status_id NUMBER := 0 ;
i_end_time VARCHAR2(1000) ;
i_total_errors NUMBER := 0 ;
i_total_warnings VARCHAR2(1000) ;
i_total_inserted NUMBER := 0 ;
i_total_updated NUMBER := 0 ;
i_total_rejected NUMBER := 0 ;
i_log_file VARCHAR2(1000) ;
i_job_run_message VARCHAR2(512) ;
i_modlast_by VARCHAR2(1000) ;
o_sqlcode NUMBER := 0 ;
o_sqlmsg VARCHAR2(1000) ;
BEGIN
$myUSPLoadJobTaskAttribs
(
i_job_id,
o_run_status_id,
o_sqlcode,
o_sqlmsg
);

END; 
/
EndOFSQL

Please advice how can i write the same in one row for each sequnce number.