Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-15-2004, 07:39 AM   #1
Registered: May 2003
Posts: 77

Rep: Reputation: 15
String manipulation with a script.


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.
Old 03-15-2004, 10:03 AM   #2
LQ Guru
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Linux Mint 17, Debian 8
Posts: 7,863

Rep: Reputation: 311Reputation: 311Reputation: 311Reputation: 311
"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.

Steve Stites
Old 03-15-2004, 11:07 AM   #3
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
I would probably use an update statement:



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"

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.
Old 03-16-2004, 10:32 AM   #4
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 199Reputation: 199
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.

Old 03-16-2004, 02:42 PM   #5
Registered: May 2003
Location: NC, USA
Distribution: Slackware 14.0
Posts: 94

Rep: Reputation: 15
save the following code as mod_tok.c and compile with make mod_tok.

#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");
		replace = argv[2];
		tok = argv[3];
 	while (fgets(line,MAXLINE,stdin))
		int i=0;
		char *t;
		t = strtok(line,tok);
			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.

Last edited by naflan; 03-17-2004 at 10:13 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
shell script: File Manipulation simon_qwl Programming 2 10-29-2005 03:40 PM
Filename string manipulation jango77 Linux - Newbie 2 03-27-2005 06:58 PM
how to manipulate string in script? ringerxyz Programming 2 02-17-2005 01:14 AM
bash + string manipulation dave bean Programming 7 02-16-2005 11:16 AM
String manipulation with a script? philipina General 1 03-15-2004 12:07 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 AM.

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