bad option(s) with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bad option(s) with awk
# 1  
Old 11-06-2009
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

Code:
#!/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[i]}'`
  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

Last edited by radoulov; 11-06-2009 at 08:55 AM.. Reason: Use code tags, please!
# 2  
Old 11-06-2009
Change the following line from:

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

to:

Code:
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.
# 3  
Old 11-06-2009
You can also do :
Code:
#!/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 :
Code:
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
# 4  
Old 11-06-2009
Quote:
Originally Posted by aigles
You can also do :
Code:
#!/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 think you mean:

Code:
IFS=_

not:

Code:
IFS=

Quote:
I have also tried :
Code:
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 ?
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:

Code:
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 Smilie

Code:
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

# 5  
Old 11-06-2009
Quote:
You should have set IFS to _ before that command.
You cannot set IFS only for the set builtin using that syntax.
On my AIX box, seting IFS for the set builting work fine (at the prompt level, not in a script file) :
Code:
> 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.
# 6  
Old 11-06-2009
Could you please post the output of the following command?

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

# 7  
Old 11-08-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can -v option in awk be used to store an array of variables?

I want to pass an array of variables to be inserted by awk in the 2nd column of a file. Empl No. Employee Age 1000000 22 1100000 24 1200000 26 Now, I want to pass an array having three different ages which need to replace the... (7 Replies)
Discussion started by: Nishi_Licious
7 Replies

2. Shell Programming and Scripting

AWK FS with ignore space option

Hi, i have in a situation to get the variables which are having a "|" delimiter and each variable may or may not have spaces, so how can i skip the spaces on the string and print the string as it is. For example: line = "|Hello world|Hello|Hi Guys| read x y z <<<$(echo "$line" | awk -F"|"... (5 Replies)
Discussion started by: tprabhaker
5 Replies

3. Shell Programming and Scripting

Why I get bad bad substitution when using eval?

Why I get bad replace when using eval? $ map0=( "0" "0000" "0") $ i=0 $ eval echo \${map$i} 0000 $ a=`eval echo \${map$i}` !!!error happens!!! bash: ${map$i}: bad substitution How to resolve it ? Thanks! (5 Replies)
Discussion started by: 915086731
5 Replies

4. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

5. Shell Programming and Scripting

-F option and - V in a single awk statment

Please let me know if I can use -F option and - V in a single awk statment. I want to import some comma separated shell variables using -F option and defining some static variables inside awk using -v option. (2 Replies)
Discussion started by: kalee
2 Replies

6. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

7. Shell Programming and Scripting

getopts: bad option(s)

Hi, I have a script that ran perfectly on Solaris 5.8 However after upgrade to Solaris 5.10 it started failing. I invoke the script as below: ./TestScript3.ksh --dir $APP_DATA_IN_OLD $NDM_DATA/$NEXT_FILE When i execute it i get the following error "getopts: dir bad option(s)". Please let... (1 Reply)
Discussion started by: JoeJoseph
1 Replies

8. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

9. Shell Programming and Scripting

awk system date with -d option

Hi I get problems when using the following command : cat logs | awk -F";" '{ system("date -d "1970-01-01 UTC+0100 $1 seconds""); }' date: date invalide `1968641199401200' date: date invalide `1968641199381709' this is what i have in my log file : cat logs 1199401200;a... (3 Replies)
Discussion started by: arag0rn
3 Replies

10. Shell Programming and Scripting

Limitations of awk? Good idea? Bad idea?

Keeping in mind that I'm relatively comfortable with programming in general but very new to unix and korn/bourne shell scripts.. I'm using awk on a CSV file, and then performing calculations and operations on specific fields within specific records. The CSV file I'm working with has about 600... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question