string manipulation in arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string manipulation in arguments
# 1  
Old 08-10-2011
string manipulation in arguments

Hi all, I have a requirement where I am taking the first argument as argument name and storing the second argument in argument name as value.
Thanks to ppl here, i learnt to do it.Smilie

Code:
while ( $1 != "" )               
    	set arg = $1
        shift
        set val = "$1"
        echo "set $arg=$val" > temp_file && source temp_file
        shift
end

Now,the format of my argument has changed, which is instead of argument name (say "./script arg1 1"), I am taking "./script -arg1 1",
I still need to save the "1" value in arg1, which is generated once i give "-arg1" as an argument.


is there anyway i can directly generate "arg1" instead of "-arg1" as my variable?

Please advice.
# 2  
Old 08-10-2011
Hi,

Try:
Code:
set arg = ${1#-}

Regards,
Birei
# 3  
Old 08-10-2011
I found a partial solution
Code:
while ( $1 != "" )               
    	set arg = "`echo $1 | sed '/-/s// /g'`"
        echo $arg
        shift
        set val = "$1"
         echo "set $arg = $val" > temp_file && source temp_file
        shift  
end

but still I am getting an error:
Code:
me@linux: csh -x ./test -a 1 -b 2
while ( -a !=  )
set arg = `echo -a | sed '/-/s// /g'`
echo -a
sed /-/s// /g
echo a
a
shift
set val = 1
echo ------------------------------------------------------arg ==  a
------------------------------------------------------arg ==  a
echo ------------------------------------------------------val == 1
------------------------------------------------------val == 1
echo set  a = 1
source temp_file
set a = 1
shift
echo you created parameter (  a ) with value ( 1 )
you created parameter (  a ) with value ( 1 )
end
while ( -b !=  )
while: Missing file name.

---------- Post updated at 02:59 PM ---------- Previous update was at 02:53 PM ----------

Hey birei

I am afraid your syntax is not working.
I am scripting in c shell, i guess it is not supported by csh.

---------- Post updated at 03:40 PM ---------- Previous update was at 02:59 PM ----------

This is the solution
Code:
while ( "$1" != "" )               
    	set arg = "`echo $1 | sed '/-/s// /g'`"
        echo $arg
        shift
        set val = "$1"
         echo "set $arg = $val" > temp_file && source temp_file
        shift  
end

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

2. Shell Programming and Scripting

string manipulation

hi all, i am new to shell scripting and need help. i have a string that stores the month in Jan/Feb/Mar format. i need to convert it to number like 01 for jan, 12 for dec etc. i am using the following sed command, echo "Enter a Month eg. Jan/Feb : " read MONTHEND ... (9 Replies)
Discussion started by: anupom2000
9 Replies

3. Shell Programming and Scripting

String manipulation

Hi All, Pls help me out on the below, 05 LAMSZ201-ZM-MEMO2-DATE02-5 PIC X(10). 05 LAMSZ201-ZM-MEMO2-AMT02-5 PIC S9(13)V99. 05 LAMSZ201-ZM-MEMO2-TYPE02-6 PIC XXX. 05 LAMSZ201-ZM-MEMO2-DATE02-6 PIC X(10). 05 ... (2 Replies)
Discussion started by: baskivs
2 Replies

4. UNIX for Dummies Questions & Answers

Help with String manipulation

Dear All, I have a question. I have files with the following pattern.>S8_SK1.chr01 NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNCAGCATGCAATAAGGTGACATAGATATACCCACACACCACACCCTAACACTAACCCTAATCTAACCCTGGCCAACCTGTTT... (13 Replies)
Discussion started by: pawannoel
13 Replies

5. Shell Programming and Scripting

string manipulation

if I have two string variable, how do I add one to anther. like a= "a" b="b" c=$a+$b but that doesn't work. Is there anyway to solve it.http://www.qtl.co.il/img/copy.pnghttp://www.google.com/favicon.icohttp://www.babylon.com/favicon.icohttp://www.morfix.com/favicon.ico (2 Replies)
Discussion started by: programAngel
2 Replies

6. Shell Programming and Scripting

String manipulation

Hi, I have the string like this ". Start : 06:53:11 - MON JUL 05, 2010" I need to print the part "06:53:11 - MON JUL 05, 2010" How i can do this? Thanks, Lenova (2 Replies)
Discussion started by: lenova2010
2 Replies

7. Shell Programming and Scripting

I need help with string manipulation

First of all I am VERY new to this so bare with me and try and explain everything even if it seems simple. Basically I want to read a line of text from a html file. See if the line of text has a certain string in it. copy an unknown number of characters (the last 4 characters wiil be ".jpg" the... (1 Reply)
Discussion started by: c3lica
1 Replies

8. UNIX for Dummies Questions & Answers

String manipulation

I am doing some training for a job I have just got and there is an exercise I am stuck with. I am not posting to ask a question about logic, just a trivial help with string manipulation. I would appreciate if somebody could at least give me a hint on how to do it. Basically, the intelligent part... (8 Replies)
Discussion started by: Dantastik
8 Replies

9. Shell Programming and Scripting

String manipulation

Hi There! I have the following block of text in my input file and in order to parse it correctly, i need to have i.e. If a line starts with a number, ignore it else replace it with the number from the previous line until the first ',' So, for example, 15,9:5/12345 ,10:1 ... (5 Replies)
Discussion started by: orno
5 Replies

10. Shell Programming and Scripting

string manipulation

Hello, I have a korn shell string variable str1 = "A,B,Z" I would like to create another korn shell string variable str2 = "letter = 'A' or letter = 'B' or letter = 'Z' " Please help! Thanks in advance an UNIX newbie! (13 Replies)
Discussion started by: hai1973
13 Replies
Login or Register to Ask a Question