• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

Script argument manipulation ...

How am I going to take an argument and then modified it and become another new parameter for my script?

Example:  my_script  A123

In my script, I shall add let say "MS" in front of argv1 and join them together and insert a "." (dot) after the third character of the argv. The new parameter will become "MSA12.3". Then I shall proceed further to manipulate this parameter.

Thanks.
0
qiang8
Asked:
qiang8
  • 4
  • 2
  • 2
  • +1
1 Solution
 
ozoCommented:
What language is your script written in?  Assuming sh:

set -- `awk 'END{print "MS"substr(x,0,3)"."substr(x,4)}' x=$1`
0
 
ahoffmannCommented:
except in ksh (not shure about bash, zsh), you cannot modify positional parameters, you need to copy them to internal variables, then see ozo's suggestion
0
 
qiang8Author Commented:
I have tried the following in csh but none can work .....

Example:  my_script A123
Contents are as follow:

1.
#!/bin/csh -f
awk 'END{print "MS"substr (x,0,3) "." substr(x,4)}' x=$1

2.
#!/bin/csh -f
awk '{print "MS"substr(x,0,3)} x=$1 '

3.
#!/bin/csh -f
awk 'END{print "MS"substr($1,0,3)"." substr($1,4)}'


Pls advise.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
ozoCommented:
set argv[1]=`awk 'END{print "MS"substr(x,0,3) "." substr(x,4)}' x=$1`
0
 
ahoffmannCommented:
set new = `echo $1 | awk '{print "MS"substr($0,0,3)"."substr($0,4)}'`
0
 
bedotCommented:
here a very basic response using regular expressions, sed & expr, well-working in all environments unix



end=`expr $1 : '.*\(.\)$'`
mid=`echo $1 | (sed -e s/.$/./)`
result=MSA$mid$end
echo $result


typîng hello, will produce MSAhell.o

beware at quotes, antiquotes, slash ans antislash
0
 
ahoffmannCommented:
bedot, is *sh syntax, not *csh,
also the argument contains no dot as you assume in your expr
0
 
qiang8Author Commented:
Thanks a lot ...
0
 
bedotCommented:
AHOFF: i don't assume dots: in a regular expression a dot represent any character
0
 
ahoffmannCommented:
oops, should use lynx, or change my browser's font
sorry for incorrectly reading, bedot.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now