LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2014, 10:46 AM   #1
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 234

Rep: Reputation: Disabled
Renaming files consecutively


I am editing a svg-file from Wikimedia Commons. The id's of the code runs like:
id="1"
id="12"
id="123"
id="1234"
id="12345"
id="123456"
id="1234567"
id="12345678" etc.

The filenames simply become too large. I want it to be:
id="1"
id="2"
id="3"
id="4" etc.

How do I do that?
Additional problem: Not all the id's in the file must be renamed.

Last edited by validator456; 03-14-2014 at 10:49 AM. Reason: addition
 
Old 03-14-2014, 10:51 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what have you tried so far ?

maybe you can reverse the string and use cut to get the first element ?
 
Old 03-14-2014, 11:06 AM   #3
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 234

Original Poster
Rep: Reputation: Disabled
I am learning regex, so I can get rid of the filenames with search and replace of Geany:
Quote:
search= id=".+"
replace= id=""
Removing the names was a breeze but renaming it....

What about replace= id="x" and x=x+1 (this is basic). Problem is that I cannot write a bash, perl or python script. I will look into reverse and cut.
 
Old 03-14-2014, 11:29 AM   #4
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 234

Original Poster
Rep: Reputation: Disabled
Replaced id with id="x"

First attempt to bash-script: (my first ever)
Quote:
#!/bin/bash
x=1

do
id=$((x+1))

done
This gives me: bashscript.sh: command not found.

Last edited by validator456; 03-14-2014 at 11:30 AM. Reason: addition
 
Old 03-14-2014, 11:35 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by validator456 View Post
Replaced id with id="x"

First attempt to bash-script: (my first ever)


This gives me: bashscript.sh: command not found.
make sure bashscript.sh is executable:
Code:
chmod 744 bashscript.sh
and call it like so:
Code:
./bashscript.sh
 
Old 03-14-2014, 11:54 AM   #6
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 234

Original Poster
Rep: Reputation: Disabled
Here is the output:
Quote:
./bashscript.sh: line 4: syntax error near unexpected token `do'
./bashscript.sh: line 4: `do'
 
Old 03-14-2014, 11:57 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
not really sure of the point of your script.
seems like you meant to write a loop but the keyword for or while are missing.
but it doesnt seem like a loop would be necessary so i would just take out the do and done.

you mite want to echo the output of the variable to see what it is doing.

Last edited by schneidz; 03-14-2014 at 11:58 AM.
 
Old 03-14-2014, 03:10 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It would be helpful to post a sample of the code you're trying to modify. Post the code in [CODE][/CODE] tags to preserve all formatting and content (important!). Make a few brief edits to show how you want the modified code to appear, and then post that separately.

Last edited by theNbomr; 03-14-2014 at 03:12 PM.
 
Old 03-15-2014, 03:25 AM   #9
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 234

Original Poster
Rep: Reputation: Disabled
I am trying to clean the code of a SVG-file to make it into a ValidSVG file.

Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   version="1.1"
   width="1351"
   height="251"
   viewBox="0 0 1500 278.016"
   id="svg2"
   xml:space="preserve"
   style="fill-rule:evenodd"><metadata
   id="metadata142"><rdf:RDF><cc:Work
       rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type
         rdf:resource="http://purl.org/dc/dcmitype/StillImage" /><dc:title></dc:title></cc:Work></rdf:RDF></metadata>
 <defs
   id="defs4"><style
     type="text/css"
     id="style6" /></defs>
 <g
   id="g3433"><path
     d="m 10.7221,124.92 0,-24.8771 11.0285,0 c 2.21836,0 3.90377,0.2265 5.05625,0.67284 1.15248,0.44522 2.07402,1.23353 2.76906,2.36269 0.688379,1.13472 1.03035,2.38601 1.03035,3.75278 0,1.76647 -0.567358,3.24981 -1.71429,4.46003 -1.14138,1.21021 -2.90785,1.98075 -5.29386,2.31162 0.874907,0.41635 1.53442,0.82827 1.98631,1.23908 0.96151,0.88045 1.87084,1.98631 2.73353,3.30755 l 4.32679,6.77054 -4.14138,0 -3.28979,-5.17839 c -0.96151,-1.48779 -1.75426,-2.63472 -2.38046,-3.42302 -0.619541,-0.79275 -1.1758,-1.349 -1.66765,-1.6621 -0.492968,-0.31866 -0.990377,-0.53849 -1.5,-0.66618 -0.376388,-0.0755 -0.984826,-0.11547 -1.83642,-0.11547 l -3.81717,0 0,11.0452 -3.28979,0 z m 3.28979,-13.8953 7.07809,0 c 1.5,0 2.68135,-0.15655 3.52739,-0.46854 0.851591,-0.3131 1.49445,-0.81162 1.93412,-1.49445 0.440785,-0.68394 0.660622,-1.42561 0.660622,-2.23057 0,-1.1758 -0.42302,-2.14286 -1.28016,-2.90118 -0.851591,-0.75944 -2.20059,-1.13583 -4.04811,-1.13583 l -7.87195,0 0,8.23057 z"
     id="path15"
     style="fill:#000000;fill-rule:nonzero" /><path
     d="M 36.184299,102.847 70.958504,83.122902"
     id="x"
     style="fill:none;stroke:#000000;stroke-width:3.75833011" /><path
     d="m 107.413,112.803 c 0,-4.13027 1.10585,-7.36232 3.3242,-9.69615 2.21836,-2.33383 5.07957,-3.50407 8.5903,-3.50407 2.29275,0 4.36677,0.5507 6.20873,1.64434 1.84197,1.10141 3.2487,2.63027 4.21688,4.59326 0.96706,1.95855 1.44782,4.18801 1.44782,6.67283 0,2.5248 -0.50407,4.78423 -1.52332,6.77054 -1.01925,1.99297 -2.46151,3.49852 -4.32679,4.52332 -1.87084,1.02591 -3.88046,1.53553 -6.04108,1.53553 -2.34604,0 -4.43671,-0.56181 -6.27868,-1.69763 -1.84752,-1.12916 -3.24315,-2.6758 -4.19356,-4.63323 -0.9493,-1.95189 -1.4245,-4.02591 -1.4245,-6.20873 z m 3.39415,0.0522 c 0,3 0.80496,5.35714 2.41488,7.08364 1.61658,1.72539 3.63731,2.58919 6.06995,2.58919 2.47927,0 4.51776,-0.86935 6.11658,-2.61251 1.59882,-1.74315 2.40377,-4.21688 2.40377,-7.42006 0,-2.02073 -0.34752,-3.78719 -1.03146,-5.29941 -0.68283,-1.51221 -1.68542,-2.68135 -3,-3.51628 -1.32013,-0.82716 -2.79793,-1.24463 -4.43671,-1.24463 -2.33383,0 -4.3379,0.79941 -6.01776,2.40377 -1.67987,1.59882 -2.51925,4.27461 -2.51925,8.01628 z"
     id="x"
     style="fill:#000000;fill-rule:nonzero" /><path
     d="M 70.958504,83.122902 103.853,101.908"
     id="x"
     style="fill:none;stroke:#000000;stroke-width:3.75833011" /><path
     d="M 134.868,102.377 168.703,83.122902"
     id="x"
     style="fill:none;stroke:#000000;stroke-width:3.75833011" /><path
     d="m 206.211,124.92 0,-24.8771 11.0285,0 c 2.21836,0 3.90377,0.2265 5.05625,0.67284 1.15248,0.44522 2.07402,1.23353 2.76906,2.36269 0.68838,1.13472 1.03035,2.38601 1.03035,3.75278 0,1.76647 -0.56736,3.24981 -1.71429,4.46003 -1.14138,1.21021 -2.90785,1.98075 -5.29386,2.31162 0.87491,0.41635 1.53442,0.82827 1.98631,1.23908 0.96151,0.88045 1.87084,1.98631 2.73353,3.30755 l 4.32679,6.77054 -4.14138,0 -3.28979,-5.17839 c -0.96151,-1.48779 -1.75426,-2.63472 -2.38046,-3.42302 -0.61954,-0.79275 -1.1758,-1.349 -1.66765,-1.6621 -0.49297,-0.31866 -0.99038,-0.53849 -1.5,-0.66618 -0.37639,-0.0755 -0.98483,-0.11547 -1.83642,-0.11547 l -3.81717,0 0,11.0452 -3.28979,0 z m 3.28979,-13.8953 7.07809,0 c 1.5,0 2.68135,-0.15655 3.52739,-0.46854 0.85159,-0.3131 1.49445,-0.81162 1.93412,-1.49445 0.44079,-0.68394 0.66062,-1.42561 0.66062,-2.23057 0,-1.1758 -0.42302,-2.14286 -1.28016,-2.90118 -0.85159,-0.75944 -2.20059,-1.13583 -4.04811,-1.13583 l -7.87195,0 0,8.23057 z"
     id="x"
     style="fill:#000000;fill-rule:nonzero" /><path
     d="m 233.858,104.202 -0.811,4.651 -1.888,0 -0.776,-4.651 0,-4.159 3.475,0 z"
     id="x"
     style="fill:#000000;fill-rule:nonzero" /><path
     d="M 168.703,83.122902 199.248,100.499"
     id="x"
     style="fill:none;stroke:#000000;stroke-width:3.75833011" /><path
     d="m 12.7761,177.517 0,-22.1869 9.83605,0 c 1.97853,0 3.48187,0.20096 4.50999,0.59956 1.02702,0.39748 1.84863,1.1003 2.46817,2.10733 0.6151,1.01258 0.920429,2.12842 0.920429,3.34752 0,1.5755 -0.506292,2.89785 -1.52998,3.97705 -1.01702,1.08031 -2.59252,1.76647 -4.72095,2.06181 0.779423,0.37194 1.36899,0.73834 1.77202,1.10474 0.857143,0.78608 1.66876,1.77202 2.43819,2.95004 l 3.85825,6.03886 -3.69282,0 -2.93449,-4.61769 c -0.857143,-1.32791 -1.56551,-2.35048 -2.12287,-3.05329 -0.552924,-0.70837 -1.04922,-1.20355 -1.48779,-1.48335 -0.439674,-0.28313 -0.88379,-0.47965 -1.3379,-0.59401 -0.336417,-0.0666 -0.878238,-0.10326 -1.63768,-0.10326 l -3.40415,0 0,9.85159 -2.93449,0 z m 2.93449,-12.393 6.31199,0 c 1.3379,0 2.39267,-0.13879 3.14656,-0.41858 0.759437,-0.27868 1.33235,-0.7228 1.72539,-1.33235 0.391932,-0.60955 0.588453,-1.27128 0.588453,-1.98853 0,-1.04922 -0.376388,-1.91192 -1.14138,-2.58808 -0.759437,-0.67727 -1.96299,-1.01258 -3.61066,-1.01258 l -7.02036,0 0,7.34012 z"
     id="x"
     style="fill:#000000;fill-rule:nonzero" /><path
     d="m 57.7295,164.474 -14.6503,0 0,-2.547 14.6503,0 0,2.547 z m 0,6.73057 -14.6503,0 0,-2.54145 14.6503,0 0,2.54145 z"
     id="x"
The file is much longer. But the start of the file gives you an idea.
Every id="x" is a new line in the SVG-file and it needs a different name. So: id="g1", id="g2", id="g3, id="g4". No problem if the file was short but this is a large one. Also notice that at the beginning of the file there are several id's that must not be renamed.
 
Old 03-15-2014, 09:27 AM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Code:
cat validator.txt | while read line
do
 if [ "`echo $line | grep id=\\"x\\"`" ]     # the \\ are for the shell to ignore literal "
 then
  #do something
 fi
done
 
Old 03-16-2014, 04:00 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Here's a good bash tutorial http://rute.2038bug.com/index.html.gz
 
Old 03-16-2014, 09:02 AM   #12
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 234

Original Poster
Rep: Reputation: Disabled
I will close this post. I have no good knowledge of bash to write my own bash-script. Thank you for your anwers.
 
Old 03-17-2014, 01:04 PM   #13
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If you're still listening, this Perl code seems to do what you need. It assumes your data is in the file 'LQvalidator456.txt', and sends the result to standard output, so it can be captured to another file.
Code:
perl -e '$x=0;while($inStr=<>){if($inStr=~m/id="x"/){$inStr=~s/x/g$x/;$x++;}print $inStr;}' LQvalidator456.txt

Last edited by theNbomr; 03-17-2014 at 01:07 PM.
 
1 members found this post helpful.
Old 03-19-2014, 06:27 AM   #14
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 234

Original Poster
Rep: Reputation: Disabled
Oh yes, I am still listening. I have tried your code and it works! Here is the proof:
https://commons.wikimedia.org/wiki/F...rrangement.svg

You have made my life much easier. Thank you very, very much.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with renaming files EchoPhyber Linux - Newbie 4 03-26-2010 08:38 AM
Renaming files mashcaster Linux - General 3 05-08-2009 03:14 PM
renaming files starwarsfan982 Linux - Software 7 10-30-2006 02:06 PM
Download consecutively numbered files patrokov Programming 15 08-23-2006 07:13 AM
crontab execute consecutively SofNine Fedora 17 07-28-2006 01:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration