Code Comments
Programming Forum and web based access to our favorite programming groups.this should be a simple problem but I can't figure it out... I have a string variable, for ex: BTID='test.testing.tested.txt' I need to split this variable in 2 : var1=''test.testing.tested' var2='txt' so that I can create the new variable that I need wich is: NEWBTID=$var1.`date +%H%M%S`.$var2 I keep thinking about awk -F "." but that doesnt work 'cause I dont know how many "." there are in $BTID Please help
Post Follow-up to this messageTom escreveu:
> this should be a simple problem but I can't figure it out...
>
> I have a string variable, for ex:
> BTID='test.testing.tested.txt'
> I need to split this variable in 2 :
> var1=''test.testing.tested'
> var2='txt'
>
> so that I can create the new variable that I need wich is:
> NEWBTID=$var1.`date +%H%M%S`.$var2
>
> I keep thinking about awk -F "." but that doesnt work 'cause I dont
> know how many "." there are in $BTID
>
Once you did above, the variable NF says how many fields there are.
For example:
$ echo 'test.testing.tested.txt' | awk -F "." '{print NF}'
4
Post Follow-up to this messageTom wrote:
> this should be a simple problem but I can't figure it out...
>
> I have a string variable, for ex:
> BTID='test.testing.tested.txt'
> I need to split this variable in 2 :
> var1=''test.testing.tested'
> var2='txt'
>
> so that I can create the new variable that I need wich is:
> NEWBTID=$var1.`date +%H%M%S`.$var2
>
> I keep thinking about awk -F "." but that doesnt work 'cause I dont
> know how many "." there are in $BTID
>
> Please help
>
Are you trying to solve an awk problem,
or is it a shell problem you are trying to use awk to solve?
I ask because you show assignments without semicolons,
as x=y (valid in both) rather than x = y (only in awk)
and use dollar signs in front of your variables many times
(a shell feature).
In awk you can use -F., then the extension is $NF.
From there you can use the sub() function to strip the
extension from $0 to get the basename.
If you don't want to use -F., use the split() function
to extract the extension.
In shell you can use things like basename to get the part
without the extension.
In most shells you can strip the leading part or the trailing
part of variables. base=${BTID%.*} ext=${BTID#${base}.}
Post Follow-up to this messageTom wrote: > this should be a simple problem but I can't figure it out... > > I have a string variable, for ex: > BTID='test.testing.tested.txt' > I need to split this variable in 2 : > var1=''test.testing.tested' > var2='txt' > > so that I can create the new variable that I need wich is: > NEWBTID=$var1.`date +%H%M%S`.$var2 > > I keep thinking about awk -F "." but that doesnt work 'cause I dont > know how many "." there are in $BTID > > Please help > This was answered by several people at comp.unix.shell a few days ago: http://tinyurl.com/fp9jy. Check the responses to articles you post before posting again. Ed.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.