Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

bash script for managing database

Posted on 2008-10-16
3
Medium Priority
?
271 Views
Last Modified: 2013-11-05
Hi folks,

I have two questions here:

(First question) basically I have a bunch of files
1.txt 2.txt 3.txt ... 100.txt and each of them is some results of
calculation done with a parameter "N".

1.txt is done with N=0.99, 2.txt is done with N=0.97, 3.txt is done
with N=0.9 4.txt is done with N=0.89...
As you can see, the filenames are named according to the sort of
parameter N.  The relation is recorded in a file called ./summary:

1    0.99
2    0.97
3    0.9
4    0.89
...
...

Now I am generating a file with N=0.98 in the very same folder.  I
want to rename all the files such that the filename still represents
the order of N, besides, I also want to update ./summary so the new N--
filename relation is updated.

(Namely, I want the new ./summary file to be
1   0.99
2   0.98
3   0.97
4   0.9
5   0.89)

(Second question)
In some other folders I have 1.gif 2.gif 3.gif .... x.gif (x differs from case to case),
and I am about to generate yet another
gif file.  The newly generated gif file is always named "new.gif"

This time I simply want bash to find the biggest number "x" of the
files, and rename my new.gif to $((x+1)).gif.
What shall I do?
0
Comment
Question by:mystea2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 5

Accepted Solution

by:
zmo earned 2000 total points
ID: 22735055
file add_new_N.sh (see code snippet below)

and now you just call 'bash add_new_N.sh'

about the second question :

for f in `dir *.gif`; do N=`cut -d'.' -f1`; done
echo $N.gif

#!/bin/bash
 
N=$1
 
echo "rm -f summary.txt"
 
for f in `dir *.txt | sed s/summary.txt//`; do 
    F=`cat $f | grep 'N=' | sed 's/N=//'`; 
    S=`echo $f | cut -d'.' -f1`
    if [ $(echo "$F > $N" | bc) -eq 0 ]; then 
        echo mv $f  $(($(echo $f | cut -d'.' -f1)+1)).txt;
        echo "echo '$(($S+1)) $F' >> summary.txt"
    elif [ $(echo "$F == $N" | bc) -eq 0 ]; then
        echo mv $f  $(($S+1)).txt;
        echo "echo '$(($S+1)) $F' >> summary.txt"
        echo "echo 'N=$N' > $f";
        echo "echo '$S $N' >> summary.txt"
    fi; 
    echo "tac summary.txt > summary.txt"
done

Open in new window

0
 
LVL 5

Expert Comment

by:zmo
ID: 22735082
of course, you have to remove all the 'echo' from the beginning of the commands statements, remove surrounding "" quotes and change '' inner quotes to ""

ie:
#!/bin/bash
 
N=$1
 
rm -f summary.txt
 
for f in `dir *.txt | sed s/summary.txt//`; do 
    F=`cat $f | grep 'N=' | sed 's/N=//'`; 
    S=`echo $f | cut -d'.' -f1`
    if [ $(echo "$F > $N" | bc) -eq 0 ]; then 
        mv $f  $(($(echo $f | cut -d'.' -f1)+1)).txt;
        echo "$(($S+1)) $F" >> summary.txt
    elif [ $(echo "$F == $N" | bc) -eq 0 ]; then
        mv $f  $(($S+1)).txt;
        echo "$(($S+1)) $F" >> summary.txt
        echo "N=$N" > $f;
        echo "$S $N" >> summary.txt
    fi; 
    echo "tac summary.txt > summary.txt"
done

Open in new window

0
 
LVL 5

Expert Comment

by:zmo
ID: 22735088
et voila :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question