Subject: | |
From: | |
Reply To: | |
Date: | Thu, 20 May 1999 13:30:20 -0700 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
Mark Wilkinson writes:
>
> Am trying to write a simple shell-script to take a variable which has three
> "parts" and break it down into three seperate variables. Maybe I'm using regexps
> wrongly here - or maybe there is a simpler way - but I just *can't* get "expr"
> to work!!
>
> Shell/iX > avar="part1 part2 part3"
> Shell/iX > expr $avar : '([^[:space:]]+)'
> Usage : expr expression
> Shell/iX >
>
> Can anyone tell me the correct regexps to use to extract part1, part2 and part3.
Your expr fails because the shell expands your original statement to:
expr part1 part2 part3 : '([^[:space:]]+)'
You can only have a single argument before the colon. What you meant to say is:
expr "$avar" : '([^[:space:]]+)'
The shell will now pass the full value of $avar to expr as a single argument.
Which doesn't give the result you need.
There's more than one way to do it.
mbloaner:/SYS/INSTALL$ avar="part1 part2 part3"
mbloaner:/SYS/INSTALL$ set -A array $avar
mbloaner:/SYS/INSTALL$ echo ${array[0]}
part1
mbloaner:/SYS/INSTALL$ echo ${array[1]}
part2
mbloaner:/SYS/INSTALL$ echo ${array[2]}
part3
Or:
mbloaner:/SYS/INSTALL$ for item in $avar; do echo $item; done
part1
part2
part3
Or:
mbloaner:/SYS/INSTALL$ echo "$avar" | sed -e 's/^\(.*\) .* .*$/\1/'
part1
mbloaner:/SYS/INSTALL$ echo "$avar" | sed -e 's/^.* \(.*\) .*$/\1/'
part2
mbloaner:/SYS/INSTALL$ echo "$avar" | sed -e 's/^.* .* \(.*\)$/\1/'
part3
Or:
mbloaner:/SYS/INSTALL$ echo "$avar" | awk '{print $1}'
part1
mbloaner:/SYS/INSTALL$ echo "$avar" | awk '{print $2}'
part2
mbloaner:/SYS/INSTALL$ echo "$avar" | awk '{print $3}'
part3
Or the one you're most interested in:
mbloaner:/SYS/INSTALL$ expr "$avar" : "\(.*\) .* .*"
part1
mbloaner:/SYS/INSTALL$ expr "$avar" : ".* \(.*\) .*"
part2
mbloaner:/SYS/INSTALL$ expr "$avar" : ".* .* \(.*\)"
part3
--
Mark Bixby E-mail: [log in to unmask]
Coast Community College Dist. Web: http://www.cccd.edu/~markb/
District Information Services 1370 Adams Ave, Costa Mesa, CA, USA 92626-5429
Technical Support Voice: +1 714 438-4647
"You can tune a file system, but you can't tune a fish." - tunefs(1M)
|
|
|