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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cut piece of a String

Status
Not open for further replies.

simpson

MIS
Oct 23, 2001
58
CA
I am attempting to cut the first part of a filename out of a variable.

Filename - servername20090201.csv
servername - all characters until 200

what I am attempting to do is this

---------------
i=servername20090201.csv
echo $i | cut -f1 -d"200"
---------------

Obviously this isn't working for me, can someone assist. The servername is not always a constant size so cut -c1-10 is not going to cut it. :)

Thanks



 
Hi

As I understand, we can count on that "200" as delimiter :
Code:
servername="${filename%200*}"
or
Code:
servername="$( echo "$filename" | sed 's/200.*//' )"

Feherke.
 
servername="$( echo "$filename" | sed 's/200.*//' )"


THIS WAS PERFECT... Thank you so much.
 
And what will your program do next year?


HTH,

p5wizard
 
sed:

[tt]servername=$(echo $i|sed 's/[1-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9]\.csv//')[/tt]

ksh:

[tt]servername=${i%[1-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9].csv}[/tt]

REs even allow for dates in the previous millennium...


HTH,

p5wizard
 
Feherke, I'm not yet re-bourne I'm afraid... ;-)
But you are absolutely right!

HTH,

p5wizard
 
How about this; in KSH.

servername=${servername%%2*}

Delete everything after and including the first occurence of 2.
Should be good for close on a thousand years ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top