For Programmers: Free Programming Magazines  


Home > Archive > AWK > July 2006 > split variable to insert date









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author split variable to insert date
Tom

2006-07-17, 6:56 pm

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

Cesar Rabak

2006-07-17, 6:56 pm

Tom 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
Jon LaBadie

2006-07-17, 6:56 pm

Tom 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}.}


Ed Morton

2006-07-17, 9:56 pm

Tom 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.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com