Moving columns around


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving columns around
# 1  
Old 06-24-2012
Moving columns around

This is a mysql night mare that I cant seem to wrap my head around. Any shell based answers is appreciated as I dont know Perl and all I would do would be blindly copy & paste !

FILE CONTENTS -
1389685 INSERT INTO Opera_ShirtCatlog(col1,col2) VALUES (1,'TEST1'),(2,'TEST2');

1389675 INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3) VALUES (9, 'Siz12, FormFit', 'Test');

DESIRED OUTPUT -

INSERT INTO Opera_ShirtCatlog(col1,col2,FIRSTCOL) VALUES (1, 'TEST', 1389685);
INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3, FIRSTCOL) VALUES (9, 'Siz12 FormFit', 'Test', 1389675);

As you can see above, the task at hand is this that I want to move the first column as part of the INSERT statement's LAST COLUMN before the closing paranthesis. I am loosing my mind trying to figure this out as I can use sed to hold the variable and then how am I supposed to substitute this only especially when it has to avoid the first closing bracket as thats table definition ... also if I use awk then how do I do a regex substitution ? Any help is sincerely appreciated.

regards,
Manohar.
# 2  
Old 06-24-2012
It is unclear to me what you mean by "the task at hand is this that I want to move the first column as part of the INSERT statement's LAST COLUMN before the closing paranthesis."

Where does FIRSTCOL come from?
# 3  
Old 06-24-2012
Fpmurphy,

At the outset thanks for posting reply. Sorry for not being clear at the outset.

In the input file, the first column is the value that I need to be moved inside the paranthesis after VALUES. In the two example line I mentioned, for the first line, 1389685 is the value that should be moved into the insert statement after the string VALUES as below -
HTML Code:
INPUT LINE 1-
1389685 INSERT INTO Opera_ShirtCatlog(col1,col2) VALUES (1,'TEST1'),(2,'TEST2');

OUTPUT LINE 1- 
INSERT INTO Opera_ShirtCatlog(col1,col2, FIRSTCOL) VALUES (1,'TEST1', 1389685),(2,'TEST2', 1389685);

INPUT LINE 2 -
1389675 INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3) VALUES (9, 'Siz12, FormFit', 'Test');

OUTPUT LINE 2 -
INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3,FIRSTCOL) VALUES (9, 'Siz12, FormFit', 'Test',1389675);

Last edited by ManoharMa; 06-24-2012 at 06:16 PM..
# 4  
Old 06-24-2012
Try this:

Code:
perl -lpe 's/^(\d+) (.*?)(\);)/$2, $1$3/g' file 
INSERT INTO Opera_ShirtCatlog(col1,col2) VALUES (1,'TEST1'),(2,'TEST2', 1389685);

INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3) VALUES (9, 'Siz12, FormFit', 'Test', 1389675);

Hope this gets you on the right path.
# 5  
Old 06-24-2012
in2nix4life,

Thank you for extending your arms in helping me. First, I dont understand PERL and second, its not moving that at all...

Any sed / awk based answer is requested from you.

regards,
# 6  
Old 06-24-2012
What OS, shell, type of file containing the data?

The more information provided, the better chance of working towards a satisfactory solution. Smilie
# 7  
Old 06-24-2012
Its a regular mysql log file and I am running bash. Also I did happen to mention early in my post that I am looking for awk/sed based solution as I dont understand PERL. Also, This is not the original file itself & after writing couple of filters, this is where it is....

HTML Code:
cat NEWOUTPUT | grep 'INSERT' | cut  -f 5- | awk '{print $2=$3=$4=""}1'
gave me the following -

HTML Code:
1389685 INSERT INTO Opera_ShirtCatlog(col1,col2) VALUES (1,'TEST1'),(2,'TEST2');
1389675 INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3) VALUES (9, 'Siz12, FormFit', 'Test');
NOw, when I tag your PERL liner, you can see below I got nothing -

HTML Code:
cat NEWOUTPUT | grep 'INSERT' | cut  -f 5- | awk '{print $2=$3=$4=""}1' |  perl -lpe 's/^(\d+) (.*?)(\);)/$2, $1$3/g'

1389685 INSERT INTO Opera_ShirtCatlog(col1,col2) VALUES (1,'TEST1'),(2,'TEST2');
1389675 INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3) VALUES (9, 'Siz12, FormFit', 'Test');
Please advise !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

2. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

3. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

4. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

5. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

7. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

8. UNIX for Dummies Questions & Answers

moving columns to alternating rows

Hello, I have a space delimited file like this: AAA BBB CCC DDD EEE FFF GGG HHH III And I would like to change it to the following (including the plus signs): AAA BBB + CCC DDD EEE + FFF GGG HHH (2 Replies)
Discussion started by: blakers
2 Replies

9. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

10. Shell Programming and Scripting

Cutting Columns and Moving in to a file

Guys, Can any one tell me how can we cut the columns and move each column in to a separate file using awk? I have a tab delimited file as shown below, 1213 wattt werree 2345 skhasdjh aasas I want to output this in to three files named a.txt,b.txt and c.txt say a.txt... (3 Replies)
Discussion started by: Serious Sam
3 Replies
Login or Register to Ask a Question