|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello all,
I have the following line: CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes I need to get the previous string to "No" string. In this example: "CSM@csUSo" How can I do it (SUN OS)? thanks in advance! |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
shulamitm wrote:
> > I have the following line: > > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes > > I need to get the previous string to "No" string. > In this example: "CSM@csUSo" > > How can I do it (SUN OS)? $ STRING="CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes" $ set -- $STRING $ echo $3 CSM@csUSo -- Best regards | Be nice to America or they'll bring democracy to Cyrus | your country. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
shulamitm wrote:
> Hello all, > > I have the following line: > > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes > > I need to get the previous string to "No" string. > In this example: "CSM@csUSo" > > How can I do it (SUN OS)? > > thanks in advance! #!/bin/sh STRING="CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes" set -- $STRING while [ "$2" != "no" ]; do shift; done echo $1 -- Best regards | Be nice to America or they'll bring democracy to Cyrus | your country. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
"shulamitm" <shulamitmi@bezeq.com> wrote in message news:4675234b-e854-4527-bdc6-b4e195f717c9@21g2000hsj.googlegroups.com... > Hello all, > > I have the following line: > > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes > > I need to get the previous string to "No" string. > In this example: "CSM@csUSo" > > How can I do it (SUN OS)? > > thanks in advance! x="CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes" echo $x | awk -F"No" '{print $1}' | sed 's/^.*Yes //' |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
"Vakayil Thobias" <vakayil.thobias@alcatel-lucent.com> wrote in message news:1199697272.957524@slbhw0... > > "shulamitm" <shulamitmi@bezeq.com> wrote in message > news:4675234b-e854-4527-bdc6-b4e195f717c9@21g2000hsj.googlegroups.com... >> Hello all, >> >> I have the following line: >> >> CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes >> >> I need to get the previous string to "No" string. >> In this example: "CSM@csUSo" >> >> How can I do it (SUN OS)? >> >> thanks in advance! > > x="CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes" > > echo $x | awk -F"No" '{print $1}' | sed 's/^.*Yes //' > echo $x | awk -F"No" '{print $1}' | awk '{print $NF}' |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Jan 7, 4:10 pm, shulamitm <shulami...@bezeq.com> wrote:
> Hello all, > > I have the following line: > > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes > > I need to get the previous string to "No" string. > In this example: "CSM@csUSo" > > How can I do it (SUN OS)? > > thanks in advance! echo $x | awk '{ for(i=1;i<=NF;i++ ) if ($i =="No") print $(i-1) }' |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
shulamitm <shulamitmi@bezeq.com> writes:
> CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes > > I need to get the previous string to "No" string. > In this example: "CSM@csUSo" > Here's a sed version sed -n -e 's:\([a-zA-Z@]*\) Yes::g' -e's:\([a-zA-Z@]*\) No:\1:pg' It works if there is more than one "No" on a line. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Maxwell Lol wrote:
> shulamitm <shulamitmi@bezeq.com> writes: > >> CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes >> >> I need to get the previous string to "No" string. >> In this example: "CSM@csUSo" >> > > Here's a sed version > > sed -n -e 's:\([a-zA-Z@]*\) Yes::g' -e's:\([a-zA-Z@]*\) No:\1:pg' sed: command garbled: s:\([a-zA-Z@]*\) No:\1:pg Add a ; before the last g. $ uname -a SunOS unknown 5.10 Generic_118855-33 i86pc i386 i86pc > It works if there is more than one "No" on a line. -- Best regards | Be nice to America or they'll bring democracy to Cyrus | your country. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On 1/7/2008 2:10 AM, shulamitm wrote: > Hello all, > > I have the following line: > > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes > > I need to get the previous string to "No" string. > In this example: "CSM@csUSo" > > How can I do it (SUN OS)? > > thanks in advance! awk -v RS=" " '/^No$/{print p}{p=$0}' Ed. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
shulamitm <shulamitmi@bezeq.com> wrote:
> Hello all, > I have the following line: > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes > I need to get the previous string to "No" string. > In this example: "CSM@csUSo" > How can I do it (SUN OS)? > thanks in advance! Shulamitm, Here is how I did it. I wrote this shell script (in /tmp/try) and called "prev": vvv #!/bin/sh prev="" echo for field in `echo $1`; do if [ "$field" = "No" ]; then echo "field previous to \"No\" is \"$prev\"" fi prev="$field" done ^^^ Here are some examples of execution: vvv joe@airlink9:/tmp/try$ ./prev "CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes" field previous to "No" is "CSM@csUSo" joe@airlink9:/tmp/try$ ./prev "No CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes" field previous to "No" is "" field previous to "No" is "CSM@csUSo" joe@airlink9:/tmp/try$ ./prev "No CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes Fred No" field previous to "No" is "" field previous to "No" is "CSM@csUSo" field previous to "No" is "Fred" ^^^ I hope this s. -Joe |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
On 2008-01-07, shulamitm wrote:
> > I have the following line: > > CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes > > I need to get the previous string to "No" string. > In this example: "CSM@csUSo" > > How can I do it (SUN OS)? In any POSIX shell, without using any external commands: line="CSM@csUCtn Yes CSM@csUSo No CSM@csUSo Yes" set -f set -- ${line%% No *} shift $(( $# - 1 )) previous=$1 -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
![]() |
| Outils de la discussion | |
|
|