1827775 Members
2518 Online
109969 Solutions
New Discussion

Re: Sed/Perl Question?

 
SOLVED
Go to solution
Allanm
Super Advisor

Sed/Perl Question?


I have a command that gives me a list of clusters in the environment like this -

/cbo/bin/clusters

cboc10
cboc18
cboc22
cboc23
cboc24
cboc26
cboc27
cboc28
cboc29
cboc30
cboc32
cboc5
cbo

There is a small glitch in the way we represent cluster 1 (cbo). Ideally it should be presented as "cboc1" instead.

I want a way to get the numerics out of the list above but strugling due to c1 glitch since it doesnt have the numeric in the end.

I want to use the command in a for loop like this -

for i in `/cbo/bin/clusters|....` #want to get the numerics only here (10,18,22...1)

Thanks,
Allan.




7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: Sed/Perl Question?

Hi Allan:

Given your data, this returns only "cbo":

# sed -n '/cbo$/p' file

Or:

# sed -ne '/cboc[0-9]*/p'

...returns everything *except* "cbo"

Regards!

...JRF...
Allanm
Super Advisor

Re: Sed/Perl Question?

Thanks for replying JRF, I want it in a way that I get cboc1 instead of just cbo.

So the output I want is -

cboc10
.
.
.
.
.
qboc1

Thanks,
Allan.
Dennis Handly
Acclaimed Contributor

Re: Sed/Perl Question?

>Ideally it should be presented as "cboc1" instead.

for i in $(< /cbo/bin/clusters); do
if [ "$i" = cbo ]; then
echo cboc1
else
echo $i
fi
done
James R. Ferguson
Acclaimed Contributor

Re: Sed/Perl Question?

Hi (again) Allan:

If I understand you, correctly you want:

# sed -ne '/cboc[0-9]\{1,\}/p' file

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Sed/Perl Question?

HI (again) Allan:

OK, I think Dennis saw through what you were asking. In keeping with my 'sed' suggestions:

# sed -e 's/\([a-z]$\)/\11/' file

...which generalizes a bit to add a trailing "1" to those entries that end in [a-z].

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Sed/Perl Question?

>JRF: to add a trailing "1" to those entries that end in [a-z].

You probably want to add "c1" to the end then you can strip off the initial "cboc" and just get the numbers:
>want to get the numerics only here (10,18,22...1)
Allanm
Super Advisor

Re: Sed/Perl Question?

Awesome guys!!

Thanks, I went in with the one liner solution, though Dennis really made it easy.

Thanks,
Allan