LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Simple C Shell script is not so simple (https://www.linuxquestions.org/questions/programming-9/simple-c-shell-script-is-not-so-simple-6523/)

elconde 09-14-2001 02:45 AM

Simple C Shell script is not so simple
 
Imagine the following program in C Shell. What should I put in place of the question marks to assign the variables a, b, c, d or any other variables like them that may show up to the last number after the last backslash.


set a = "a/b/c/d/1/2/3"
set b = "e/r/r/4"
set c = "dew/gr/34/op3/22"
set d = "33/22/11/1?"
.
.
.

??????????????????????


So, I want to set the variables a, b, c, d to 3, 4, 22, and 1?. I tried using awk, but it was a disaster.

HELP!!!

crabboy 09-16-2001 10:20 PM

I would use bourne shell, C shell stinks when writing scripts. Use awk to find the number of slashes in the line, then use cut to cut out the last field. You will need to set the field seperator for awk. I tried it quickly here and awk didn't like the '/' as a delimiter. You can probably hack at it a bit to make it work.
Code:

FIELDS=`echo $A | awk ' { FS = "/"; print NF } '`
LAST=`echo $A | cut -d'/' -f$FIELDS`

This does not work, but I hope it will get you started.

elconde 09-16-2001 11:53 PM

thanks
 
Thanks for the advice. I'll try it out.


-Robert


All times are GMT -5. The time now is 01:31 AM.