| Author |
Message |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: 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
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
Bruce B

Joined: 18 May 2005 Posts: 10816 Location: The Peoples Republic of California
|
Posted: 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
|
|
 |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: 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.
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
Bruce B

Joined: 18 May 2005 Posts: 10816 Location: The Peoples Republic of California
|
Posted: 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
|
|
 |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: 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
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
Bruce B

Joined: 18 May 2005 Posts: 10816 Location: The Peoples Republic of California
|
Posted: 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
|
|
 |
Bruce B

Joined: 18 May 2005 Posts: 10816 Location: The Peoples Republic of California
|
Posted: 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
|
|
 |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: 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.
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: Sat 08 Sep 2012, 05:05 Post subject:
|
|
And this is the Application
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
SFR

Joined: 26 Oct 2011 Posts: 570
|
Posted: 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
|
|
 |
rcrsn51

Joined: 05 Sep 2006 Posts: 7742 Location: Stratford, Ontario
|
Posted: 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
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1757
|
Posted: 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
|
|
 |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: 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
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
rcrsn51

Joined: 05 Sep 2006 Posts: 7742 Location: Stratford, Ontario
|
Posted: Sat 08 Sep 2012, 12:04 Post subject:
|
|
[Edit] My mistake.
|
|
Back to top
|
|
 |
stu91

Joined: 06 Aug 2012 Posts: 100 Location: England. Dpup. Dell Inspiron 1501
|
Posted: 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
|
|
 |
|