Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

very simple substitution

Status
Not open for further replies.

cbsm

Programmer
Oct 3, 2002
229
FR
Hello,
I have to write a very simple shell (I am not used to this at all).
I used grep to put a line from a file in a variable like so :
Suivi_Id=$(grep "Suivi_Id" ${FILE})

echo $Suivi_Id prints : Suivi_Id123

I want only 123

so I tried (and tried, and tried...)

for example : Suivi_Id=${$Suivi_Id:8}
or Suivi_Id=${Suivi_Id:8}

result (both cases) : bad substitution

I also tried
l_Suivi=`expr length $Suivi_Id`
Suivi_Id= `expr substr $Suivi_Id 8 $(l_Suivi)`
result (both lines) expr: syntax error

I tried with $ or not, with {or( or not ...

I know it must be some very simple and stupid thing ...
Thank you !
 
Hi. You might find few ideas and variations in:

Thread822-811273

HTH.

All I ask of you
Is make my wildest dreams come true
 

Thank you !
I tried a few, but they are not working either - or better said I can't make them work !
In the thread they are printing the result.
i need the result in a variable.
For example I tried echo "$Suivi_Id" | cut -c9-30
(this is not exactly what I want, but at this stage woud be OK)
this works perfectly
But how do I do something like
Suivi_Id = "$Suivi_Id" | cut -c9-30
here : error Suivi_Id=123: not found
I tried
Suivi_Id = `"$Suivi_Id" | cut -c9-30}`
(same error)
and
Suivi_Id = {"$Suivi_Id" | cut -c9-30}
(cut: invalid character in range)

[I liked the expression like so :Suivi_Id=${Suivi_Id:8}
because I need everything BUT the 8 first charchters - but any solution will do]


 
Suivi_Id = `"$Suivi_Id" | cut -c9-30}`

Is that a direct paste? If so, lose the extraneous } at the end.

All I ask of you
Is make my wildest dreams come true
 
Thank you - but this was only an error in my post ...
Should read : `"$Suivi_Id" | cut -c9-30`
 
Maybe try:

`echo "$Suivi_Id" | cut -c9-30`

All I ask of you
Is make my wildest dreams come true
 
I tried ....
again an error message ...
123: not found
(123 is the result I am waiting for - but $Suivi_Id has still the wrong value)
 
Hi

No idea what shell and tools you use...
Code:
[blue]master #[/blue] Suivi_Id=Suivi_Id123
[blue]master #[/blue] Suivi_Id=${Suivi_Id:8}
[blue]master #[/blue] echo $Suivi_Id
123
[blue]master #[/blue] Suivi_Id=Suivi_Id123
[blue]master #[/blue] Suivi_Id=`echo $Suivi_Id | cut -c9-`
[blue]master #[/blue] echo $Suivi_Id
123
[blue]master #[/blue] Suivi_Id=Suivi_Id123
[blue]master #[/blue] Suivi_Id=`echo $Suivi_Id | sed 's/.\{8\}//'`
[blue]master #[/blue] echo $Suivi_Id
123

Feherke.
 

Just tried again - must have writen something wrong :
Suivi_Id=`echo "$Suivi_Id" | cut -c9-30` is working !

Many, many thanks !
 
Hi

With [tt]ksh[/tt] the first ( in my above list does not work, but the [tt]cut[/tt] and [tt]sed[/tt] solutions should work regardless the shell. For [tt]ksh[/tt] :
Code:
[blue]master #[/blue] Suivi_Id=Suivi_Id123
[blue]master #[/blue] Suivi_Id=${Suivi_Id#????????}
[blue]master #[/blue] echo $Suivi_Id
123

Feherke.
 
Not in front of a server to test, but these should work with ksh93:

Code:
var=Suivi_Id123
print ${var##*[Aa-Zz]}
print ${var:8:3}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top