Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 22 Sep 2014, 10:15
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Split Index and File Name from string?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 2 [21 Posts]   Goto page: 1, 2 Next
Author Message
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Fri 07 Sep 2012, 23:51    Post subject:  Split Index and File Name from string?
Subject description: How to do?
 

Hi.

I have list files containing strings like: 59170-"LazY-Fred-English-Locals.tar.gz" which is the last entry in one list file.

This command COUNT=$((`tail -1 $INDEXFILE | cut -d "-" -f1`)) gives me the 59170 as a result in $COUNT - coming from the last enry of the list file.

Let's say i have a single string (no file) 59170-"LazY-Fred-English-Locals.tar.gz" in $OUTSTR

What do i have to change in COUNT=$((`tail -1 $INDEXFILE | cut -d "-" -f1`)) to get the Index and File Name (without the double quotes) as a result in $INDEX and $FILENAME?

INDEX= ? ? ?
FILENAME= ? ? ?

Thanks

RSH

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
Bruce B


Joined: 18 May 2005
Posts: 11108
Location: The Peoples Republic of California

PostPosted: Sat 08 Sep 2012, 01:48    Post subject:  

It could be easier if all your 'indexes' were 5 digits like in the example above. Are they? If so, then something like this:
Code:
STRING="59170-LazY-Fred-English-Locals.tar.gz"

FILENAME=`echo $STRING | sed 's/^.....//'`

COUNT=`echo $STRING | cut -d - -f 1`

#test content of variables

echo \$STRING = $STRING 
echo \$FILENAME = $FILENAME
echo \$COUNT = $COUNT 



testing outputs

$STRING = 59170-LazY-Fred-English-Locals.tar.gz
$FILENAME = -LazY-Fred-English-Locals.tar.gz
$COUNT = 59170

_________________
New! Puppy Linux Links Page

Last edited by Bruce B on Sat 08 Sep 2012, 02:09; edited 1 time in total
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Sat 08 Sep 2012, 01:58    Post subject:  

Bruce B wrote:
It could be easier if all your 'indexes' were 5 digits like in the example above. Are they?

~

Unfortunately not.

Is there any bash function that gives me the position of - ? I could do the rest using string functions that i know and have already used. Just how to find the - ! Could do it in a loop, thought there would be any easier way to do that.

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
Bruce B


Joined: 18 May 2005
Posts: 11108
Location: The Peoples Republic of California

PostPosted: Sat 08 Sep 2012, 02:11    Post subject:  

I think a loop would be helpful

Please post about 10 or more actual filenames

~

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Sat 08 Sep 2012, 02:52    Post subject:  

Bruce B wrote:
I think a loop would be helpful

Please post about 10 or more actual filenames

~

Hi Bruce B.

Thanks for your code example. Please take the files below. Would be nice, to have a smarter solution.

How I did solve this:
Code:
INDEXSTR="108-"grubfd01.zip""
#INDEXSTR="1168-"FreeSans.zip""
#INDEXSTR="58992-"LazY-FReD-1.0.2.sfs.gz""
#INDEXSTR="58993-"LazY-FReD-1.0.2.pet""
#INDEXSTR="59170-"LazY-Fred-English-Locals.tar.gz""
nlen="`echo ${#INDEXSTR}`"
echo $nlen

doloop="true"
start=0
while $doloop; do
   s0=${INDEXSTR:$start:1}
   echo $s0
   if [ "$s0" = "-" ]; then
      doloop="false"
   fi
   ((start++))
   echo $start
done

s1=${INDEXSTR:0:$start-1}
s2=${INDEXSTR:$start-1:$nlen-$start+1}
echo $s2
nlen="`echo ${#s2}`"
echo $nlen
s3=${s2:1:$nlen-1}
echo $s1
echo $s3

I wonder, if it could be done to make this shown code a function in a single script that would be called from another script and would return $s1 and $s3 ---> or similar $COUNT and $FILENAME ?
RSH

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
Bruce B


Joined: 18 May 2005
Posts: 11108
Location: The Peoples Republic of California

PostPosted: Sat 08 Sep 2012, 03:28    Post subject:  

what the files have in common is the - after the numbers.

to get just the numbers you could use cut -d - -f 1

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
Bruce B


Joined: 18 May 2005
Posts: 11108
Location: The Peoples Republic of California

PostPosted: Sat 08 Sep 2012, 03:36    Post subject:  

I don't understand what you are trying to accomplish, if I knew it would be most helpful.

BTW if you want to remove quotes in the file name in the variable

I have a better idea, don't use quotes or any special characters or spaces. I don't and it makes writing scripts to handle files much easier.

~

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Sat 08 Sep 2012, 03:50    Post subject:  

I am working on a program that can download the attached files from murga forum, selectable from a list in a gui. Therefor i read out the database - using attach&id , which is in each download link of each attached file.

After building the index file i do get the listed names as a combination of the index and the file name, formatted as shown: 108-"grubfd01.zip"

To download the file i do need the number of its index. But the file is been downloaded as: viewtopic.php?mode=attach&id=108 which gives me absolutely no information on what file type it is. Therefor i do need the file name. After downloading as viewtopic.php?mode=attach&id=108 i move the file and give it a new name ---> the file name.

This is the Project of it all.

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Sat 08 Sep 2012, 05:05    Post subject:  

And this is the Application Very Happy
_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
SFR


Joined: 26 Oct 2011
Posts: 1068

PostPosted: Sat 08 Sep 2012, 06:12    Post subject:  

RSH wrote:
Bruce B wrote:
I think a loop would be helpful

Please post about 10 or more actual filenames

~

Hi Bruce B.

Thanks for your code example. Please take the files below. Would be nice, to have a smarter solution.

How I did solve this:
Code:
INDEXSTR="108-"grubfd01.zip""
#INDEXSTR="1168-"FreeSans.zip""
#INDEXSTR="58992-"LazY-FReD-1.0.2.sfs.gz""
#INDEXSTR="58993-"LazY-FReD-1.0.2.pet""
#INDEXSTR="59170-"LazY-Fred-English-Locals.tar.gz""
nlen="`echo ${#INDEXSTR}`"
echo $nlen

doloop="true"
start=0
while $doloop; do
   s0=${INDEXSTR:$start:1}
   echo $s0
   if [ "$s0" = "-" ]; then
      doloop="false"
   fi
   ((start++))
   echo $start
done

s1=${INDEXSTR:0:$start-1}
s2=${INDEXSTR:$start-1:$nlen-$start+1}
echo $s2
nlen="`echo ${#s2}`"
echo $nlen
s3=${s2:1:$nlen-1}
echo $s1
echo $s3

Maybe something like this:

Code:
#!/bin/bash

INDEXSTR="108-"grubfd01.zip""
#INDEXSTR="1168-"FreeSans.zip""
#INDEXSTR="58992-"LazY-FReD-1.0.2.sfs.gz""
#INDEXSTR="58993-"LazY-FReD-1.0.2.pet""
#INDEXSTR="59170-"LazY-Fred-English-Locals.tar.gz""

COUNT=`echo $INDEXSTR | cut -d '-' -f1`
FILENAME=`echo $INDEXSTR | cut -d '-' -f2-`

echo $INDEXSTR
echo $COUNT
echo $FILENAME


RSH wrote:
I wonder, if it could be done to make this shown code a function in a single script that would be called from another script and would return $s1 and $s3 ---> or similar $COUNT and $FILENAME ?

As far as I know there's no a simple way to communicate between scripts.
You can use a temporary file (script1 writes to -> script2 reads from) or you can try this:
http://www.murga-linux.com/puppy/viewtopic.php?t=75778
I never tried it, however, so don't know how exactly use it.

Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send private message 
rcrsn51


Joined: 05 Sep 2006
Posts: 9125
Location: Stratford, Ontario

PostPosted: Sat 08 Sep 2012, 11:14    Post subject:  

RSH wrote:
I wonder, if it could be done to make this shown code a function in a single script that would be called from another script and would return $s1 and $s3 ---> or similar $COUNT and $FILENAME ?
RSH

If script1 sends its data to stdout using echo statements, then script2 can retrieve it with code like
Code:
OUT=$(script1)
COUNT=$(echo $OUT | cut -d " " -f 1)
FILENAME=$(echo $OUT | cut -d " " -f 2)
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2247

PostPosted: Sat 08 Sep 2012, 11:33    Post subject:  

All these loops and extraneous use of echo, sed and cut... Bash makes this very simple:
Code:

INDEX=${OUTSTR%%-*}
FILENAME=${OUTSTR#*-}
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Sat 08 Sep 2012, 11:54    Post subject:  

amigo wrote:
All these loops and extraneous use of echo, sed and cut... Bash makes this very simple:
Code:

INDEX=${OUTSTR%%-*}
FILENAME=${OUTSTR#*-}

Thanks amigo.

Looks like this could be what i'm looking for. Will test it later.

RSH

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
rcrsn51


Joined: 05 Sep 2006
Posts: 9125
Location: Stratford, Ontario

PostPosted: Sat 08 Sep 2012, 12:04    Post subject:  

[Edit] My mistake.
Back to top
View user's profile Send private message 
stu91


Joined: 06 Aug 2012
Posts: 145
Location: England. Dpup. Dell Inspiron 1501

PostPosted: Sat 08 Sep 2012, 13:03    Post subject:  

amigo wrote:
All these loops and extraneous use of echo, sed and cut... Bash makes this very simple:
Code:

INDEX=${OUTSTR%%-*}
FILENAME=${OUTSTR#*-}


Hi Amigo,
Could you give a breakdown on what the different characters represent in you code - or any links etc that might expand on such code further.

Thanks in advance.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 2 [21 Posts]   Goto page: 1, 2 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0903s ][ Queries: 12 (0.0059s) ][ GZIP on ]