Fetch a section from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch a section from a file
# 1  
Old 07-30-2012
Lightbulb Fetch a section from a file

Hi,
I have a file like...

Code:
 
$cat file1
+++++++++++++++++++ client1 +++++++++++++++++++++++++++++
col1 col2 col3
------ ----- -----
(0 rows affected)
=========================================================
+++++++++++++++++++ client1 +++++++++++++++++++++++++++++
col1 col2 col3
------ ----- -----
(0 rows affected)
=========================================================
+++++++++++++++++++ client1 +++++++++++++++++++++++++++++
col1 col2 col3
------ ----- -----
(0 rows affected)
=========================================================
+++++++++++++++++++ client1 +++++++++++++++++++++++++++++
col1 col2 col3
------ ----- -----
val1 val2 val3
val11 val22 val33
(2 rows affected)
=========================================================
+++++++++++++++++++ client1 +++++++++++++++++++++++++++++
col1 col2 col3
------ ----- -----
(0 rows affected)
=========================================================

I need a code that will output only the section in bold...
i.e only the section starting from "++++" till the "rows affected)"

In other words, the section where the no. of rows affected is greater than 0

Code:
 
$script.sh file1
+++++++++++++++++++ client1 +++++++++++++++++++++++++++++
col1 col2 col3
------ ----- -----
val1 val2 val3
val11 val22 val33
(2 rows affected)

Can anyone please help me ?
code can be either in shell/perl
# 2  
Old 07-30-2012
Will this do?
Code:
awk '/[1-9][0-9]* rows affected/' RS=\= infile


Last edited by elixir_sinari; 07-30-2012 at 11:29 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 07-30-2012
thanks, it worked had to increase no. of equal to signs though
# 4  
Old 07-31-2012
Fetch a section from a file

Hi Exilir,

Can you please let me know that how this code works specially record sperater?

Code:
 
awk '/[1-9][0-9]* rows affected/' RS=\= infile

Thanks
Krsna.
# 5  
Old 07-31-2012
The record separator has been set as a = here as the input is such that the required "section" ends with a series of =. Since for my version of awk only the first character of RS value is significant, I could not put in those many = as mentioned by the OP. But, other awks (gawk, etc.) would most probably take in those many = as RS.

Both alternatives would yield identical results. The only difference is the overhead in doing the work. With a single = as RS, the number of records read in would be much greater (and most records would contain nothing!) than that with a series of = as RS.

Last edited by elixir_sinari; 07-31-2012 at 10:47 AM..
# 6  
Old 08-01-2012
Fetch a section from a file

Hi Exilir.

Thanks a lot got it. Here action is not mentioned means by default it will print whole record
Code:
$0 .


Thanks
Krsna..
# 7  
Old 08-01-2012
Alternate awk
Code:
awk '/client/{++c}c==4 && !/[=]/{print}' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change file section into each line?

Hi Gurus, I have below file which has different sections, need to move the sections to beginning of the each record. original file aaa bbb ccc ddd eee fff output file. aaa bbb ccc ddd eee fff (6 Replies)
Discussion started by: green_k
6 Replies

2. Shell Programming and Scripting

How to segregate a section from big file?

Hello, I need to know all IP range (ip_prefix), associated with us-west-2 region only from this link - https://ip-ranges.amazonaws.com/ip-ranges.json (it can be opened in wordpad for better visibility) Please suggest, how would I do it. If vi, awk or sed is needed, I have downloaded it on my... (7 Replies)
Discussion started by: solaris_1977
7 Replies

3. Shell Programming and Scripting

awk to lookup section of file in a range of another file

In the below, I am trying to lookup $1 and $2 from file1, in a range search using $1 $2 $3 of file2. If the search key from file1 is found in file2, then the word low is printed in the last field of that line in the updated file1. Only the last section of file1 needs to be searched, but I am not... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

Adding a lines to specific section of the file.

Hello, I have to a add 2 lines to /etc/sudoers file under this section below, can someone please suggest script to add these two lines when execute this remotely on to a multiple servers. before ## Allow root to run any commands anywhere root ALL=(ALL) ALL After ## Allow root... (2 Replies)
Discussion started by: bobby320
2 Replies

5. Shell Programming and Scripting

Delete a section of a file if...

i have a file as below that has n section : 2006 0101 1236 49.3 L 37.902 48.482 0.0 Teh 5 0.2 2.7LTeh 1 GAP=238 E Iranian Seismological Center, Institute of Geophysics, University of Tehran 6 ... (5 Replies)
Discussion started by: oreka18
5 Replies

6. Shell Programming and Scripting

Read section of file and take decision

Hi, My requirement is that I've to read a config file having below info Oracle number|double;integer -> user can put multiple values here blob|binary varchar2|string varchar|string number(p,s)|decimal timestamp|date/time and so on .... Sybase bigint|bigint datetime|date/time... (2 Replies)
Discussion started by: dips_ag
2 Replies

7. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

8. Shell Programming and Scripting

cutting a section of a big file

Hi, I have a text file 10giga size. Opening the file with vi takes forever ... Im intersting only with the 100 first records. Is there way to copy those 100 lines to new file (with no need to open the file)? Thanks (6 Replies)
Discussion started by: yoavbe
6 Replies

9. Shell Programming and Scripting

Using Sed to duplicate a section of a file....

hello all, I have a file like this: section 1 blah1 blah2 section 2 blah1 blah2 section 3 blah1 blah2 and I want to use sed to duplicate section 2, like this: section 1 blah1 blah2 section 2 blah1 blah2 section 2 blah1 (2 Replies)
Discussion started by: nick26
2 Replies

10. UNIX for Dummies Questions & Answers

help find a section line of a file

hi, I have a 20 line file. I need a command which will brinf back a specific line based upon the line number I enter. e.g. the file looks like this and is called file1 jim is a man john is a woman james is a man wendy is a woman lesley is a woman i want a command that will... (4 Replies)
Discussion started by: sureshy
4 Replies
Login or Register to Ask a Question