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. |
|
 |
03-15-2004, 07:39 AM
|
#1
|
|
Member
Registered: May 2003
Posts: 77
Rep:
|
String manipulation with a script.
Hello,
I'm a newbie in linux and I have a question about linux script and string manipulation.
In my case I have file with a dump of a mysql database.
ex: INSERT INTO test_table VALUES ('first','1','','','test');
I would like to change this line to add a new parameter.
For example, if I need to add the parameter 'A' in fourth position I will have to count the number of comma (3) and insert the letter 'A'.
My question is. Is it possible to do this with a simple script. If yes, what can I use as command to count a special charater and insert a small string after x special charaters?
Thanks in advance.
|
|
|
|
03-15-2004, 10:03 AM
|
#2
|
|
Guru
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Squeeze, Fedora 14
Posts: 7,268
Rep:
|
"what can I use as command to count a special charater and insert a small string after x special charaters?"
You can probably do this with the sed command. See:
man sed
___________________________________
Be prepared. Create a LifeBoat CD.
http://users.rcn.com/srstites/LifeBo...home.page.html
Steve Stites
|
|
|
|
03-15-2004, 11:07 AM
|
#3
|
|
Moderator
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,816
Rep: 
|
I would probably use an update statement:
Code:
#!/bin/sh
TABLE_NAME="test_table"
IFS='
'
for i in `cat data.dump | grep $TABLE_NAME | grep INSERT`; do
KEY=`echo $i | cut -d'(' -f2 | cut -d',' -f1 `
echo "UPDATE $TABLE_NAME set fourth_col = 'A' where first_col = $KEY"
done
Otherwise you will have to parse out the data for each column and build you insert statement. Since you don't mess with the existing data with the update script you are less likely to corrupt it.
|
|
|
|
03-16-2004, 10:32 AM
|
#4
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
What exectly are you trying to do?
change a MySQL dump file?
do you want a general solution or a one off?
if it's a one-off, just use sed.
billy
|
|
|
|
03-16-2004, 02:42 PM
|
#5
|
|
Member
Registered: May 2003
Location: NC, USA
Distribution: Slackware 13.0
Posts: 91
Rep:
|
save the following code as mod_tok.c and compile with make mod_tok.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE 1000
int main(int argc, char *argv[])
{
int index;
char *tok;
char *replace;
char line[MAXLINE];
char new_line[MAXLINE];
if(argc < 4){
printf("Usage: mod_tok INDEX, REPLACE, TOKEN\n");
exit(1);
}
else{
index=atoi(argv[1]);
replace = argv[2];
tok = argv[3];
}
while (fgets(line,MAXLINE,stdin))
{
int i=0;
char *t;
strcpy(new_line,line);
t = strtok(line,tok);
if(strncmp(line,"INSERT",6)){
printf("%s",new_line);
continue;
}
while(t){
if(i) printf("%s",tok);
if (index == i++) printf("%s",replace);
else printf("%s",t);
t = strtok(NULL, tok);
}
}
}
To run
./mod_tok 3 \\'A\\' , < example.sql > newfile.sql
To change 'first',
./mod_tok 1 second \\' < example.sql > newfile.sql
I'm sure there are sql commands to do what you need as well.
naflan
Last edited by naflan; 03-17-2004 at 10:13 AM.
|
|
|
|
| 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 10:29 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
|
|