The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
option followed by : taking next option if argument missing with getopts gurukottur Shell Programming and Scripting 2 03-17-2008 12:46 PM
-n option ravi raj kumar Shell Programming and Scripting 1 01-03-2008 09:20 AM
what is gcc -e option in C useless79 High Level Programming 3 12-05-2007 01:36 PM
su option lesstjm UNIX for Advanced & Expert Users 1 11-02-2005 01:54 PM
cut -f option 435 Gavea UNIX for Dummies Questions & Answers 1 11-10-2003 05:50 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 2 Weeks Ago
rajeshorpu rajeshorpu is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 29
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; 2 Weeks Ago at 08:55 AM.. Reason: Use code tags, please!
  #2 (permalink)  
Old 2 Weeks Ago
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
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 (permalink)  
Old 2 Weeks Ago
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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 (permalink)  
Old 2 Weeks Ago
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by aigles View Post
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

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 (permalink)  
Old 2 Weeks Ago
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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 (permalink)  
Old 2 Weeks Ago
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
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 (permalink)  
Old 2 Weeks Ago
rajeshorpu rajeshorpu is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 29
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.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0