cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] Any bash scripting experts?

Community Veteran
Posts: 6,773
Thanks: 257
Fixes: 20
Registered: 16-02-2009

[SOLVED] Any bash scripting experts?

I am trying to cut a substring from a file
e.g.
File a contains a load of stuff along with a description I want
sample
           
Quote
aaa.zzzz.start.filename.date.cutme.rubbish

What I want to do is strip out the
         
Quote
start.filename.date

and use it to rename another file
So far I have
for i in ./*
do
a=$(grep start <$i |cut -d'.' -f3-)
b=$(expr index "$a" cutme)

echo "(echo $a | cut  -b 1-$b)"
but the b variable is sometimes retuning the wrong position for the cutme substring.
9 REPLIES
Community Veteran
Posts: 6,773
Thanks: 257
Fixes: 20
Registered: 16-02-2009

Re: Any bash scripting experts?

Ok fixed it

c=$(awk -v c="$a" -v d="$b" 'BEGIN{print index(c,d)}')
if [ "$c" != "0" ]
then
c=$((c-2))
e=$(echo $a|cut -b 1-$c)

Index only looks for chars not strings.
VileReynard
Seasoned Pro
Posts: 10,999
Thanks: 265
Fixes: 11
Registered: 01-09-2007

Re: Any bash scripting experts?

You could just locate the positions of the 2nd & 5th dots - and use substring to join the first part to the second part...
I think  sed could do that?

Community Veteran
Posts: 6,773
Thanks: 257
Fixes: 20
Registered: 16-02-2009

Re: Any bash scripting experts?

Trouble was/is that the string was NOT always 3 long, it might be 2 or 10, something like
rubbish.good.good.good.good.date.rubbish
rubbish.good.good.good.good.good.good.date.rubbish
My solution works on that. b$ is the start of the rubbish at the end (a constant)
alanb
Grafter
Posts: 459
Registered: 24-05-2007

Re: Any bash scripting experts?

Bash always makes my brain hurt, so I haven't read your code properly. I'd rather do this sort of thing using the Python language, it would be much easier.
If I have understood correctly, you are trying to find the start and end of the file name to extract it from the text file as a complete string.
I think I might approach it differently by taking the entire line from the  text. Then use the CUT command to split the line into an array of fields by splitting on every '.' character. Throw away the fields I don't need, using pattern matches or whatever makes most sense. Then reassemble the file name from the fields I do want to keep, putting the '.' delimiters back at the same time.
Just tell me to shut it up, if I've gone off in completely the wrong direction.  Cheesy
Community Veteran
Posts: 6,773
Thanks: 257
Fixes: 20
Registered: 16-02-2009

Re: Any bash scripting experts?

I tried that splitting it in to an array then piecing it back together again but the awk method is simpler.
c results in a number that is the sting position of the finial rubbish part, then I use cut to substring it from 1 to that position -2 (-2 so it goes back before the . to the last char I want)
I test to make sure the sting was found - I was bitten by a couple having different bits at the end. Then I end up with the string in e ready to to a mv on..
I would probably have written in it C but it seemed like a trivial thing to do in bash  Smiley
linux
Grafter
Posts: 146
Registered: 23-08-2007

Re: [SOLVED] Any bash scripting experts?

I'm a bit late to this topic, but bash is able to do this job internally via some (admittedly cryptic) parameter expansion.
For example:
% a="aaa.zzzz.start.filename.date.cutme.rubbish"
% b="${a##*.start.}"
% c="${b%%.cutme.*}"
% echo $c
filename.date
%

The line starting "a=" just gets the string into variable a.
The line starting "b=" removes the longest matching prefix "*.start.".
The line starting "c=" removes the longest matching suffix ".cutme.*"
The result is "filename.date".
Obviously you can experiment as required.
Using bash parameter expansion like this would be preferable if (a) awk/python/etc. were not available, or (b) performance was important (parameter expansion should be faster than running separate commands).
VileReynard
Seasoned Pro
Posts: 10,999
Thanks: 265
Fixes: 11
Registered: 01-09-2007

Re: [SOLVED] Any bash scripting experts?

But what if the start or cutme delimiters are unknown, because they are variable?

linux
Grafter
Posts: 146
Registered: 23-08-2007

Re: [SOLVED] Any bash scripting experts?

hmm, I had presumed that the words "start" and "cutme" were present in the input file.
If that is not the case then it might still be possible to use parameter expansion to remove the prefix/suffix, depending on the exact format of the input file. Admittedly the format of the input file is somewhat less than crystal clear to me.
Perhaps the input file could be preprocessed with sed to make it more suitable for parameter expansion.
Otherwise, yes, awk/python/etc. would have to be used.
Community Veteran
Posts: 6,773
Thanks: 257
Fixes: 20
Registered: 16-02-2009

Re: [SOLVED] Any bash scripting experts?

Quote from: Un
But what if the start or cutme delimiters are unknown, because they are variable?

The delimiters are not variable they are static, the bit in between is the variable.
@linux I did try using that method but couldn't get it working inside a script it worked fine on a cmd line.
As I said this was a quick and dirty one but it works so I am happy with it.