chun
chun

Reputation: 847

how can i use regex to get a certain string of a file

with linux bash shell , how can i use regex to get a certain string of a file

by example:

for filename *.tgz do

"get the certain string of filename (in my case, get 2010.04.12 of file 2010.01.12myfile.tgz)"

done

or should I turn to perl

Merci

frank

Upvotes: 0

Views: 501

Answers (3)

fpmurphy
fpmurphy

Reputation: 2537

FILE=2010.01.12myfile.tgz

echo ${FILE:0:10}

gives

2010.01.12

Upvotes: 1

topskip
topskip

Reputation: 17335

#!/bin/sh
a="2010.04.18Myfile.tgz"
echo ${a%%+([a-zA-Z.])}

bash' regexp are quite powerful (at least compared to standard sh or command.com :-))

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342363

with bash, for the simplest case, if you know what you want to get is a date stamp, you can just use shell expansion

#!/bin/bash

for file in 20[0-9][0-9].[01][0-9].[0-9][0-9]*tgz
do
 echo $file
done

else, if its anything before the first alphabet,

for file in *tgz
do
 echo ${file%%[a-zA-Z]*}
done

otherwise, you should spell out your criteria for the search.

Upvotes: 1

Related Questions