Regex to Parse data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex to Parse data
# 8  
Old 06-28-2012
Looks like a broken regular expression parser. ) should not be treated as a special character in an extended regular expression unless it occurs outside a bracket expression in conjunction with a preceeding ( that is also outside a bracket expression.

If this fails ...
Code:
awk 'BEGIN {s ~ /)/}'

but this succeeds ...
Code:
awk 'BEGIN {s ~ /\)/}'

... then I'm almost certainly correct.

\) is technically an undefined sequence, but that workaround has been known to work with some buggy implementations.

Regards,
Alister
# 9  
Old 07-02-2012
Hi Everyone,

For some strange reason, I cant seem to get an answer on a topic that pales in comparison with some of the tough questions that have been posed here.

At any rate, after mucking with what was posted here by elixir_sinari here is what I got.

/Users/ManoharChandran 21:36:21 $cat ANOTHER | awk -F\( 'BEGIN{OFS=FS} {for(i=2;i<=NF;i++) sub(/\)/,", NEWCOLUMN)",$i);print}'
1389675 Opera_ShirtCatalog INSERT INTO Opera_ShirtCatalog(COL1, COL2, NEWCOLUMN) VALUES (1, 'TEST1', NEWCOLUMN), (2,'TEST2', NEWCOLUMN);
1389685 Opera_ShirtCatlog_Wom INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3, NEWCOLUMN) VALUES (9,'Siz12, FormFit', 'Test', NEWCOLUMN);

Now, the LAST REMAINING ISSUE is I dont want the NEWCOLUMN in the values, I want the first value to be moved as "VALUE" to be inserted ... essentially what I need is below -

HTML Code:
/Users/ManoharChandran 21:36:31 $cat ANOTHER | awk -F\( 'BEGIN{OFS=FS} {for(i=1;i<=NF;i++) sub(/\)/,", NEWCOLUMN)",$i);print}' 	
Opera_ShirtCatalog INSERT INTO Opera_ShirtCatalog(COL1, COL2, NEWCOLUMN) VALUES (1, 'TEST1', 1389675), (2,'TEST2', 1389675);	
Opera_ShirtCatlog_Wom INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3, NEWCOLUMN) VALUES (9,'Siz12, FormFit', 'Test', 1389685);
NOT


/Users/ManoharChandran 21:36:31 $cat ANOTHER | awk -F\( 'BEGIN{OFS=FS} {for(i=1;i<=NF;i++) sub(/\)/,", NEWCOLUMN)",$i);print}'
1389675 Opera_ShirtCatalog INSERT INTO Opera_ShirtCatalog(COL1, COL2, NEWCOLUMN) VALUES (1, 'TEST1', NEWCOLUMN), (2,'TEST2', NEWCOLUMN);
1389685 Opera_ShirtCatlog_Wom INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3, NEWCOLUMN) VALUES (9,'Siz12, FormFit', 'Test', NEWCOLUMN);

If someone can push this over this last bump, I would be truly grateful.

Please advise !

regards,
Manohar.

Last edited by ManoharMa; 07-02-2012 at 01:49 AM..
# 10  
Old 07-02-2012
I had deliberately used "NEWCOLUMN" instead of any value as I didn't know the logic behind inserting those particular values (and still don't know it Smilie)...
# 11  
Old 07-02-2012
I presumed that you understood what needs to be done in here ... Anyway, let me rehash as to what is needed .... sorry if this wasnt clear to begin with...

Lets look at first ROW, the first column, 1389675 is the value of the NEWCOLUMN and as such, it has to be inserted in the table ... well let me show you clearly as to what is the input and what is the expected output....

INPUT-

1389675 Opera_ShirtCatalog INSERT INTO Opera_ShirtCatalog(COL1, COL2) VALUES (1, 'TEST1'), (2,'TEST2');
1389685 Opera_ShirtCatlog_Wom INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3) VALUES (9,'Siz12, FormFit', 'Test');


OUTPUT -

INSERT INTO Opera_ShirtCatalog(COL1, COL2, NEWCOLUMN) VALUES (1, 'TEST1', 1389675), (2,'TEST2', 1389675);
INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3, NEWCOLUMN) VALUES (9,'Siz12, FormFit', 'Test', 1389685);

I saved and viewed this couple of times to make sure that necessary values are properly highlighted and please bear with me on this as I m new to this edit / highlighting ... If something is not clear, please let me know and I shall respond right away...

Once again thank you Elixir_sinari.

Last edited by ManoharMa; 07-02-2012 at 02:28 AM..
# 12  
Old 07-02-2012
I am sorry for not seeing the value. I thought it was just another part of the line..Smilie

Anyway, this should work:

Code:
awk -F\( 'BEGIN{OFS=FS} {split($1,a," ");sub(/.*/,"INSERT INTO "a[2],$1);for(i=2;i<=NF;i++)i==2?sub(/)/,", NEWCOLUMN)",$i):sub(/)/,", "a[1]")",$i);print}'  inputfile

In any case, Chubler_XL's solution is much simpler and better than this. I'd rather use his solution for your purpose than this one.

Last edited by elixir_sinari; 07-02-2012 at 03:06 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 13  
Old 07-02-2012
elixir_sinari,

Can u please explain this to me as to what exactly are you doing here as this bit as this is working finally....

Now, the bigger question...why cant we do this using SED ? I got it working up to the last portion as below -

Code:
/Users/ManoharChandran 00:37:37 $cat ANOTHER | grep 'INSER' | sed 's/) VALUES/,NEWCOLUMN) VALUES/g'
1389675	Opera_ShirtCatalog INSERT INTO Opera_ShirtCatalog(COL1, COL2,NEWCOLUMN) VALUES (1, 'TEST1'), (2,'TEST2');
1389685	Opera_ShirtCatlog_Wom INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3,NEWCOLUMN) VALUES (9,'Siz12, FormFit', 'Test');

The only pending thing in the above statement is mark up the first set of numbers and move them inside the closing brackett ... can u please come up with something on that line as that is what I need. Also, that is the reason as to why I titled it as REGEX help coz I couldnt come up with the mover for the closing bracket when there are more than one closing brackets

I understand as I did not get what you are doing inside the awk statement in here ...

Btw, thanks a lot for your help !

regards,
Manohar.

Last edited by ManoharMa; 07-02-2012 at 03:47 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Need command to parse data

Hi Friends, I have data like below t064266 I want output into this format t064266 Data are space delimited and i want parse third column data. Thanks (9 Replies)
Discussion started by: Jagaat
9 Replies

2. Shell Programming and Scripting

Perl :: to parse the data from a string.

Hi folks, I have a line in log from which I need to parse few data. Jul 6 00:05:58 dg01aipagnfe01p %FWSM-3-106011: Deny inbound (No xlate) From the above... I need to parse the %FWSM-3-106011: substring. Another example Jul 13 00:08:55 dq01aipaynas01p %FWSM-6-302010: 2 in use, 1661... (3 Replies)
Discussion started by: scriptscript
3 Replies

3. Shell Programming and Scripting

RegeX to parse data from a txt file

Hi all the experts out there, I am totally new to perl and I was given an assignment by using Perl to find the 2nd element of each line in each curly bracket which made up of 5 elements. Expected result should like this: Type: VCC Pin_name: AK32,AL32,AH21,..... Type: NC Pin_name:... (2 Replies)
Discussion started by: killbanne
2 Replies

4. Shell Programming and Scripting

Parse data

Guys , please help me out with another AWK solution ... Input Device Physical Name : Not Visible Device Symmetrix Name : 0743 Front Director Paths (2): { ---------------------------------------------------------------------- ... (5 Replies)
Discussion started by: greycells
5 Replies

5. Shell Programming and Scripting

Parse data

hi i have a file p1.htm <div class="colorID2"> aaaa aaaa aa <br/> bbbbbbbb bbb<br/> <br/>cccc ccc ccc </div><div class="colorID1"> dddd d ddddd<br/> eeee eeee eeeeeeeeee<br/> fffff <br/>g gg<br/> (5 Replies)
Discussion started by: saw7
5 Replies

6. Shell Programming and Scripting

How to parse data?

Hi all, I have output of paction command looking like this: RELCI 0 IP address 1.2.16.3 Xmit: CURRENT Recv: WAIT_HEADER 0 congestions 2617/0 buf. sent/rec Xmit: CURRENT Recv: WAIT_HEADER 0 congestions 0/0 buf. sent/rec BUFFER Xmit: ... (6 Replies)
Discussion started by: sameucho
6 Replies

7. Shell Programming and Scripting

Extract and parse data between two strings

Hi , I have a billing CDR file which is separated by “!”. I need to extract and format data between the starting (“!”) and the end of the line (“1.2.1.8”). These two variables are permanent tags to show begin and end. ! TICKET NBR : 2 ! GSI : 101 ! 3100.2.112.1 24/03/2010 00:41:14 !... (3 Replies)
Discussion started by: jaygamini
3 Replies

8. Shell Programming and Scripting

regex/shell script to Parse through XML Records

Hi All, I have been working on something that doesn't seem to have a clear regex solution and I just wanted to run it by everyone to see if I could get some insight into the method of solving this problem. I have a flat text file that contains billing records for users, however the records... (5 Replies)
Discussion started by: Jerrad
5 Replies

9. Shell Programming and Scripting

parse data using sh script

Hi, I am a newbie to unix/shell scripting and i have a question on how to parse a txt file using perl in a sh script. I have a txt file that contains hundreds of lines with data like this.... X, Y, Latitude, Longitude 1, 142, -38.000000, -91.000000, 26.348 2, 142, 60.000000, -90.000000,... (2 Replies)
Discussion started by: moonbaby
2 Replies

10. Shell Programming and Scripting

Parse a range of data

Hello, I have a file which has a range of date like: 00:00 test 00:01 test2 00:02 test3 00:03 test4 00:04 test5 00:05 test6 Using input (stdin) i would like to parse the data 00:01 to 00:04. The output file should be like this: 00:01 test2 00:02 test3 00:03 test4 00:04 test5 ... (5 Replies)
Discussion started by: BufferExploder
5 Replies
Login or Register to Ask a Question