awk printing only parts of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk printing only parts of file
# 1  
Old 01-27-2012
awk printing only parts of file

I am afraid I don't understand awk well enough to do the following.

I have a file with a bunch of select statements where the a line starts off with this pattern:
"Last parsed SQL statement :", then continues with the select statement. At the first blank space I'd like it to stop, print that entire select statement, and then ignore the rest of the file until it comes to the next select.
Only print the select statements, and add a newline between each one.

Some of these selects are duplicates it would be nice if the dups could be removed, but not as necessary.
The file pattern is like this:

Last parsed SQL statement :
select and then a multi line query
the rest of the query
....

Informix Dynamic Server Version 10.00.FC5 -- On-Line -- Up 2 days 21:32:45 -- 19055584 Kbytes

Sess SQL Current Iso Lock SQL ISAM F.E.
Id Stmt type Database Lvl Mode ERR ERR Vers Explain
1060733 - sentryprod NL Not Wait 0 0 9.03 Off

Last parsed SQL statement :
select and then a multi line query
the rest of the query
....

Any help is greatly appreciated !

Thanks,
floyd
# 2  
Old 01-27-2012
Please provide a more realistic data rather than "..." and "rest of query" etc
We need to know where it ends and how does the next line looks like and all.

A guess work... Try this...
Code:
awk '/Last parsed/,/^ *$/{if(!/Last parsed/)print}' infile

--ahamed
# 3  
Old 01-27-2012
Here is a more exact model of what the file looks like.
I just replaced the actual sql with jibberish.

Last parsed SQL statement :
update sometable set somecolumn=somevalue where
somevalue in (select somecolumn from sometable where
somevalue='something' )


IBM Informix Dynamic Server Version 10.00.FC5 -- On-Line -- Up 2 days 21:32:45 -- 19055584 Kbytes

Sess SQL Current Iso Lock SQL ISAM F.E.
Id Stmt type Database Lvl Mode ERR ERR Vers Explain
1060733 - sentryprod NL Not Wait 0 0 9.03 Off

Last parsed SQL statement :
select somthing from some table where something=something and
something else=something else


IBM Informix Dynamic Server Version 10.00.FC5 -- On-Line -- Up 2 days 21:32:45 -- 19055584 Kbytes
...
....
# 4  
Old 01-27-2012
Did you try the code in post#2? And do you want only the select statements or all the statements?

--ahamed
# 5  
Old 01-27-2012
Quote:
Originally Posted by ahamed101
Did you try the code in post#2? And do you want only the select statements or all the statements?

--ahamed
I am stupid. I didn't think that would work, because it looked like all you were printing was the line where Last parsed was.
Shows you how little I understand awk.

So I just tried your exact code and it looks like it gave me exactly what I want.
Thank you !! I will attempt to study that.

Do you know if there is a way to use awk to remove the duplicate sql statements ?
# 6  
Old 01-27-2012
Removing duplicate is possible, but you need to tell us or show us how the statements look like. For e.g. do they have the exact syntax and spacing etc.

Paste an input with duplicate sql statements and we'll give it a try!

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to clean up input file, printing both fields

In the f1 file below I am trying to clean it up removing lines the have _tn_ in them. Next, removing the characters in $2 before the ninth /. Then I remove the ID_(digit- always 4). Finally, the charcters after and including the first _. It is curently doing most of it but the cut is removing $1... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Improve awk code that has three separate parts

I have a very inefficient awk below that I need some help improving. Basically, there are three parts, that ideally, could be combined into one search and one output file. Thank you :). Part 1: Check if the user inputted string contains + or - in it and if it does the input is writting to a... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

[awk] printing value of a variable assignment from a file

Heyas Me try to print only the value of a (specific) variable assignment from a file. What i get (1): :) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME dot-blue "" "$TUI_DIR_INSTALL_ROOT/usr" "$TUI_DIR_INSTALL_ROOT/etc/tui" "$TUI_PREFIX/share/doc/tui" "$TUI_PREFIX/share/tui"... (2 Replies)
Discussion started by: sea
2 Replies

4. Shell Programming and Scripting

Printing from file with awk

Hi, I'm trying to find an efficient way with awk to print the lines from a file which match the string 'OFFLINE' and DO NOT match the string ONLINE within 2 minutes from the date stamp in column 1. The file is similar to the following: 09:40:26 HostC OFFLINE 09:40:43 HostA ... (7 Replies)
Discussion started by: keenboy100
7 Replies

5. Shell Programming and Scripting

Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number: 2013101000. The last two digits are serial number (00). So maximum of serial number is 100. After reaching 100 it becomes 00 with incrementing 10 which is day with max 31. after reaching 31 it becomes 00 and increments 10... (31 Replies)
Discussion started by: Natalie
31 Replies

6. Shell Programming and Scripting

awk - printing the passwd file

I've got a number of RHEL systems and I'm trying to use awk to read and format the output of /etc/passwd. But I'd like to display the host name of the system at the beginning of each line of output. I've got it working without the adding the host name in this script: #!/bin/bash ... (3 Replies)
Discussion started by: westmoreland
3 Replies

7. UNIX for Dummies Questions & Answers

Printing File Contents Using AWK

I want to print the contents of a file from 2nd line to last but one line using the AWK command. I tried using the Shell Script,MyScript as fallows: MyScript: { if(NR>1) { if(NR<9) { print $0 } } } and used the commnd : awk -f MyScript Filename Note: Filename contains 9 lines text.... (0 Replies)
Discussion started by: ashok.g
0 Replies

8. Shell Programming and Scripting

SED printing just parts of lines that match an expression

Hi - I am guessing this is fairly simple for someone .. but I can not quite figure it out. I need a sed command to print just parts of lines from a file. e.g. filea.txt 4710451 : Success : MODIFY : cn=user1,dc=org,dc=uk Message log started Message log ended 4710452 : Success : MODIFY :... (7 Replies)
Discussion started by: sniper57
7 Replies

9. Shell Programming and Scripting

need help with awk in printing the fields in a file

hi all i get a file from a server and i do not know how many fields that file will contain. i need to cut from the second column of the file to the last, irrespective of how many fields are there. is there any way to make the awk command dynamic for fetching from the second to the last... (4 Replies)
Discussion started by: sais
4 Replies

10. Shell Programming and Scripting

filter parts of a big file using awk or sed script

I need an assistance in file generation using awk, sed or anything... I have a big file that i need to filter desired parts only. The objective is to select (and print) the report # having the string "apple" on 2 consecutive lines in every report. Please note that the "apple" line has a HEX... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question