Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

split variable to insert date
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


Report this thread to moderator Post Follow-up to this message
Old Post
Tom
07-17-06 11:56 PM


Re: split variable to insert date
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

Report this thread to moderator Post Follow-up to this message
Old Post
Cesar Rabak
07-17-06 11:56 PM


Re: split variable to insert date
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}.}



Report this thread to moderator Post Follow-up to this message
Old Post
Jon LaBadie
07-17-06 11:56 PM


Re: split variable to insert date
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
07-18-06 02:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:20 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.