[SOLVED] Any bash scripting experts?
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Other forums
- :
- Tech Help - Software/Hardware etc
- :
- [SOLVED] Any bash scripting experts?
[SOLVED] Any bash scripting experts?
19-06-2012 1:24 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: Any bash scripting experts?
19-06-2012 1:51 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: Any bash scripting experts?
20-06-2012 12:23 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
I think sed could do that?
"In The Beginning Was The Word, And The Word Was Aardvark."
Re: Any bash scripting experts?
20-06-2012 9:44 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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)
Re: Any bash scripting experts?
20-06-2012 12:22 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: Any bash scripting experts?
20-06-2012 1:57 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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
Re: [SOLVED] Any bash scripting experts?
07-07-2012 3:22 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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).
Re: [SOLVED] Any bash scripting experts?
07-07-2012 5:43 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
"In The Beginning Was The Word, And The Word Was Aardvark."
Re: [SOLVED] Any bash scripting experts?
07-07-2012 6:13 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: [SOLVED] Any bash scripting experts?
08-07-2012 10:03 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Other forums
- :
- Tech Help - Software/Hardware etc
- :
- [SOLVED] Any bash scripting experts?