Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-02-2005, 08:02 PM
|
#1
|
|
Member
Registered: Dec 2004
Posts: 145
Rep:
|
Shell script: substitute a file's content according to a map?
Now I want to substitute some strings to other strings in one file according to a map. For example, the map is:
Code:
A STR1
B STR2
C STR3
...
and the file is:
Code:
......<$A>....<$C>....
...<$C>....<$B>.....$<A>
............<$B>......
Now I can only use shell script to get one line every time and do substitution:
Code:
_map_num=`wc -l $_map | cut -d" " -f1`
while [ $i -le $_map_num ]; do
line=`sed -n "$i p" $_map`
<Do substitution by $line>
i=`expr $i + 1`
done
But I think this is not efficient because the sed will process the $_map file every time to get a line. Is there any way that I can just achieve the goal to process 2 files in 1 time just use awk? Or is there any other ways to make it more efficient?
Thanks.
|
|
|
|
11-02-2005, 10:09 PM
|
#2
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,973
|
Here's a sed example that does every occurence of src string in all tgt files:
Code:
sed -i -e 's/Internalitem_code/internal_item_code/g' *.sql
HTH
|
|
|
|
11-03-2005, 07:46 PM
|
#3
|
|
Member
Registered: Dec 2004
Posts: 145
Original Poster
Rep:
|
look at this code:
Code:
_map_num=`wc -l $_map_file | cut -d" " -f1`
_map=`cat $_map_file`
if [ -z $_result ]; then _result=`cat $_tpl`; fi
i=1
while [ $i -le $_map_num ]; do
line=`echo "$_map" | sed -n "$i p"`
# echo "$_map" | while read line; do
The "sed" can works but I think it's not efficient enough. But "read" takes no effect.
What's wrong?
|
|
|
|
11-03-2005, 08:02 PM
|
#4
|
|
Member
Registered: Dec 2004
Posts: 145
Original Poster
Rep:
|
More detail, I have written a script to generate SQL from a template and a string map(I will use it to generate SQL automatically and query MySQL database with "mysql" continously for testing in my project). So the next time, if I will test another database, I don't need to change the code, but give a different template and map.
The template like this:
Code:
BEGIN;
SET INSERT_ID=SELECT MAX(UID) FROM ACCSTORE<$NUM> + 1;
# SET INSERT_ID=<$INSERT_ID>;
INSERT INTO `ACCSTORE<$NUM>` ( ACCOUNT , PASSWD , STATE , TYPE) VALUES( '<$ACCOUNT>' , '<$PASSWD>' , '0' , '0');
INSERT INTO `BASEINFO<$NUM>` ( ACCOUNT , ADDRESS , BIRTH , CREATETIME , EMAIL , IDCARD , MPHONE , MPTYPE , NICKNAME , PHONE , POSTNUM , SEX , SUPERPASSWD , TNAME , TOKENRING , UID) VALUES( '<$ACCOUNT>' , '' , '' , '' , '<$NAME>@<$DOMAIN>' , '' , '' , '0' , '' , '' , '' , '0' , '<$NAME>' , '' , '<$MD5>' , 'SELECT MAX(UID) FROM ACCSOTRE<$NUM> + 1');
INSERT INTO `POINTBONUS<$NUM>` ( ACCOUNT , UID) VALUES( '<$ACCOUNT>' , 'SELECT MAX(UID) FROM ACCSTORE<$NUM> + 1');
COMMIT;
This database have 7 types tables, and every type have 31 tables. There is a hash program to determine which to insert.
and The map file like this:
Code:
NUM: cmd(random_int 0 32 %04d)
ACCOUNT: cmd(random_str "a b c d e f g h i j k l m n o p q r s t u v w x y z - _ @ 1 2 3 4 5 6 7 8 9 0")
PASSWD: cmd(random_str "a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 0 - = _ + @ % , . ; A B C D E J G H I J K L M N O P Q R S T U V W X Y Z")
NAME: user
DOMAIN: example.com.cn
MD5: cmd(random_str "a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 0 - = _ + @ % , . ;" | md5sum | cut -d" " -f1)
STR: cmd(random_str "a b c d e f g h i j k l m n o p q r s t u v w x y z - _ @ 1 2 3 4 5 6 7 8 9 0 - = + % , . ÖÜ Åô ÖÐ ÎÄ ; Ǩ¤ ¨®¨¨ ¨®Ò ÂÌ Ë¨° Ò СÀ ¡¤ç ϸ Ó¨º ²» Ш¨ ¹¨¦ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z")
DATE: cmd(date -d "`random_str 0 $RANDOM` days ago" -I)
NUSER: new_user_name
NDOMAIN: example.com
IDNUM: cmd(expr $(random_int 0 1000000))
The script will looks up the map and finds the srings to substitute, if it is cmd(.*), then it will execute it as a shell command by "eval", and the result will be the string to replace. The script like this:
Code:
#!/bin/sh
# ¸Ã½Å¡À¾ÀûÓÃÒ»¸ö SQL Ä£¡ãæºÍÒ»¸öÕë¶Ô¸ÃÄ£¡ãæµÄÓ³Éäɨ²³É SQL ²¨¦Ñ¯Óï¾äÒÔ¹©²âÊÔÖ®Óá£
# Ä£¡ãæÖÐÓÐһЩ <$STR> ÐÎʽµÄ´®£¬Í¡§¹ýÔÚÓ³ÉäÎļþÖв¨¦ÕÒϨ¤Ó¦µÄ STR ¶øµÃµ½Ìæ»»¡£
# ÕâÀïÀûÓÃÁË´¨²Âëɨ²³ÉơµÄÔÀ¨ª¡£
# ÖÜÅô, Chowroc at atgame, 20051102
_tpl=""
_map_file="map.txt"
_map=""
__result=""
# ÒÔϺ¯Êý²¨²É¨²Ö¸¶¡§¡¤¶Î¡ìÖ®ÄÚµÄËæ»¨²ÕûÊý
random_int() {
start=$1
range=`expr $2 - $1`
if [ $# -eq 3 ]; then
format=$3;
else
format="%d"
fi
num=`echo "" | awk "{srand(); print int(rand()*$range)+$start; }"`
printf "$format" $num
}
# ÀûÓÃÒÔϺ¯Êý²¨²É¨²Ò»¸öËæ»¨²µÄ¡À䳡è¡ÁÖ¡¤û´®
random_str() {
len="length()"
if [ $# -eq 2 ]; then len=$2; fi
echo $1 \
| sed 's/ /\n/'g \
| while read L; do echo "$L $RANDOM"; done \
| sort -k2n \
| cut -d" " -f1 \
| while read L; do echo -n $L; done \
| awk "{srand(); num=int(rand()*$len)+1; print substr(\$0,0,num)}"
# | awk "{srand(); num=int(rand()*length())+1; print substr(\$0,0,num)}"
# | awk '{srand(); num=int(rand()*length())+1; print substr($0,0,num)}'
echo
}
usage() {
echo "usage: $0 -f template [-m map]"
}
while [ $# -ge 1 ]; do
if [ $1 == "-f" ]; then
_tpl=$2;
# Èç¹û $2 Ϊ¿Õ£¬ÄÇôÏÂÃæµÄ wc ºÍ cat »¨¢Ê¹³ÌШ°Í£Ö¹µÈ´ý´Ó¡À¨º¡Á¼ÊäÈë
shift
elif [ $1 == "-m" ]; then
_map_file=$2;
shift
# elif [ $1 == "-" ]; then
# _result=``
fi
shift
done
# if [ $# -eq 0 ]; then
if [ -z $_tpl ] || [ -z $_map_file ]; then usage; exit 0; fi
if ! [ -f $_tpl ]; then
echo "No such template file."
exit 1
fi
if ! [ -f $_map_file ]; then
echo "No such map file."
exit 1
fi
_map_num=`wc -l $_map_file | cut -d" " -f1`
_map=`cat $_map_file`
if [ -z $_result ]; then _result=`cat $_tpl`; fi
# Ó¦¸Ã³¢ÊÔ´Ó¡À¨º¡Á¼ÊäÈë¶ÁÈ¡¿´ÄÜ¡¤ñ̨¢¸ßÖ´ÐÐСìÂÊ£¡
i=1
while [ $i -le $_map_num ]; do
# line=`sed -n "$i p" $_map_file`
line=`echo "$_map" | sed -n "$i p"`
# ÿ´Î¶ÁÈëÒ»ÐÐÓ³Éä
# echo "$_map" | while read line; do
# cat $_map_file | while read line; do
# echo "$line"
if echo $line | grep "cmd\(.*\)" >/dev/null 2>&1; then
cmd=`echo $line | sed "s/^.*cmd(\(.*\))$/\1/g"`
src=`echo $line | awk -F: '{print $1}'`
dst=`eval $cmd`
else
src=`echo $line | awk -F: '{print $1}'`
dst=`echo $line | awk -F: '{print $2}' | sed 's/^ *//g'`
# ÕâÀïÓà sed È¥³ýÐÐÊ¡ÁµÄ¿Õ¸ñ¡£¸¨¹ºÃµÄ¡ã¨¬¡¤¡§£¿
fi
# ¸¨´¾ÝǨ¦¿ö£¬Èç¹ûÓÐ cmd(.*)£¬Ô¨°½«Æä¡Á¡ÂΪ shell è¹ÁîÖ´Ðв¢½«¡¤µ»Ø½¨¢¹û¡Á¡ÂÎªÌæ»»´®
# echo "<\$$src> --> $dst"
echo "<\$$src>"
echo $dst
# _result=`echo "$_result" | sed "s/<\$$src>/$dst/g"`
_result=$(echo "$_result" | sed "s/<\$$src>/$dst/g")
# ½øÐÐÌæ»»
i=`expr $i + 1`
done
echo "$_result"
Any suggestion? I have said that now the problem is "sed" is not efficient, but "read" takes no effect.
Thanks
|
|
|
|
11-04-2005, 03:20 AM
|
#5
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
I like it  great idea.
I think maybe you are stretching the limits of shell programming here.
When you are starting to use lots of pipes to sed, awk, cut, grep
it starts getting very inefficient and messy.
maybe you should consider using perl?
OR (not so good but did you know?)
You can use m4 for simple substitution.
maybe first pass substitutions second pass evals?
you can substitue with m4 command line like so:
Code:
m4 -DA=STR1 -DB=STR2 -DC=STR3 template
or put the definitions in a file.
|
|
|
|
11-04-2005, 03:26 AM
|
#6
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
suggestion:
how about getting random words from a dictionary
( /usr/dict/words ? ) instead of just random letters?
|
|
|
|
11-06-2005, 05:09 AM
|
#7
|
|
Member
Registered: Dec 2004
Posts: 145
Original Poster
Rep:
|
Before I have tried to get random words from /usr/share/dict/linux.words, also slow for the same reason.
Now it is about generating 1 SQL transaction per sec(90s~2min for 105 transactions). I tested using "while read", It could only make it faster about 10~20 seconds, and if I use:
echo "$_result"
in the "while read", I could get the right result every cycle, but I don't know how to get it from the circulation with high performance!
And I think anothor problem is that: it must assign the _result variable every time, and this make the low efficiency.
Maybe I will rebuild it with python, I'm learning that. But before that, I want to try to test the concurrency: If I could run 20 copies of that script at the same time, what about the result?
Thank's for you help. :-)
Last edited by Chowroc; 11-06-2005 at 05:26 AM.
|
|
|
|
11-07-2005, 05:14 AM
|
#8
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
Quote:
|
Before I have tried to get random words from /usr/share/dict/linux.words, also slow for the same reason.
|
you could always try converting it to a dbm database if you have time, it's very simple and very quick.
|
|
|
|
11-07-2005, 07:41 AM
|
#9
|
|
Member
Registered: Dec 2004
Posts: 145
Original Poster
Rep:
|
Quote:
Originally posted by bigearsbilly
you could always try converting it to a dbm database if you have time, it's very simple and very quick.
|
Could you give me some details?
I have tried the concurrency, and no effect. Even I write a simple C program that fork child processes to do the task!
In fact, I found that if I fork 5 child processes, The program will wait about 5 seconds, and 20 seconds for 20 childs. The execution becames not averagely, and the total time was near to the results before.
I don't know the exact reason.
Thank you very much.
|
|
|
|
11-08-2005, 03:56 AM
|
#10
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
example? for /usr/dict/words into DBM database?
I have some dbm code somewhere, It could be changed to make a simple
random word generator. Is this what you mean?
|
|
|
|
11-14-2005, 08:45 PM
|
#11
|
|
Member
Registered: Dec 2004
Posts: 145
Original Poster
Rep:
|
Quote:
Originally posted by bigearsbilly
example? for /usr/dict/words into DBM database?
I have some dbm code somewhere, It could be changed to make a simple
random word generator. Is this what you mean?
|
Yes, that what I mean. thank you.
|
|
|
|
11-15-2005, 04:08 AM
|
#12
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
ok I will dig it out.
it's at home so laters.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:33 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|