Parsing Comma separated Arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing Comma separated Arguments
# 1  
Old 11-15-2009
Parsing Comma separated Arguments

Hi unix guru's

I want to execute a shell script like ksh printdetails.ksh Andy,Bob,Daisy,Johnson

like passing all the four names in the as the arguments and these arguments are varies between 1 to 10.
How to pass these names to the shell script variable.
and also i want to know the count of arguments passed to script.

Thanks in advance.
# 2  
Old 11-15-2009
Here's one way.

Code:
#!/bin/ksh

VARS=$1

set -A A_NAME $( echo "$VARS" | sed 's/,/ /g' )

NUM_VARS=${#A_NAME[@]}

while [ -n "${A_NAME[i]}" ]
do
   echo "name = ${A_NAME[i]}"
   (( i = i + 1 ))
done

echo "Total Names:  ${NUM_VARS}"


Code:
./names.ksh mike,bill,jim,john

name = mike
name = bill
name = jim
name = john

Total Names:  4

# 3  
Old 11-15-2009
And here is another way:
Code:
#!/bin/ksh
oIFS="$IFS"; IFS=, ; set -- $1 ; IFS="$oIFS"
for i in "$@"; do
  echo name = $i
done
echo Total Names: $#

Code:
$>./test Cooper,"Billy Bob",Trevor,Smithers
name = Cooper
name = Billy Bob
name = Trevor
name = Smithers
Total Names: 4


Last edited by Scrutinizer; 11-15-2009 at 03:01 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 11-15-2009
Hi Scrutinizer

is there any way that we can do this job workout $1
because when i use $1 it was defaultly assuming my environment variable
# 5  
Old 11-16-2009
Hi Reddy, I do not understand what you mean exactly, could you be specific and/or give an example?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract fields from a CSV i.e comma separated where some of the fields having comma as value?

can anyone help me!!!! How to I parse the CSV file file name : abc.csv (csv file) The above file containing data like abv,sfs,,hju,',',jkk wff,fst,,rgr,',',rgr ere,edf,erg,',',rgr,rgr I have a requirement like i have to extract different field and assign them into different... (4 Replies)
Discussion started by: J.Jena
4 Replies

2. Shell Programming and Scripting

Parsing Comma Separated values to UNIX variable from PLSQL

Hi All, I'm trying to pass the comma separated values (string) returned from Plsql Procedure to UNIX variable. getting the below log message cat: -: Bad file descriptor awk: cmd. line:1: fatal: error reading input file `-': Bad file descriptor The output coming from plsql procedure is... (3 Replies)
Discussion started by: Mahesh3089
3 Replies

3. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

4. Shell Programming and Scripting

Need comma separated output

Hi, I am having the file with server names & its corresponding process, i need your help how to convert into comma separated output between server & app #cat apps.txt Server1 oracle was Server2 http webadmin Server3 tsm db2 My requirement is like below. Server1,oracle/was... (5 Replies)
Discussion started by: ksgnathan
5 Replies

5. Shell Programming and Scripting

Needs help in parsing comma separated values

hello experts, i am retrieving values in variables jobKey and jobName within my shell script. these values are returned to me within braces and i am using following command to remove those braces: jobKeys=`echo $jobKeys | sed 's:^.\(.*\).$:\1:'` jobNames=`echo $jobNames | sed... (1 Reply)
Discussion started by: avikaljain
1 Replies

6. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

7. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

8. Shell Programming and Scripting

Parsing and filtering multiline text into comma separated line

I have a log file that contains several reports with following format. <Start of delimiter> Report1 header Report1 header continue Report1 header continue Record1 header Record1 header continue Record1 header continue field1 field2 field3 field4 ------... (1 Reply)
Discussion started by: yoda9691
1 Replies

9. Shell Programming and Scripting

parsing comma separated file

Hi, I have a file with th elist of patches separated by comma, like below: patch1, patch 2, patch 3................ t\The number of patches is not known as it changes every time. I need assistance in writing a routine such as it will take patch1 as first variable and performs the... (4 Replies)
Discussion started by: avikaljain
4 Replies

10. Shell Programming and Scripting

Need comma separated processing

I have a file like this OUTLN OPEN Y SCOTT OPEN N USER4 OPEN Y DBSNMP EXPIRED & LOCKED N ... (4 Replies)
Discussion started by: ilugopal
4 Replies
Login or Register to Ask a Question