Pulling x number of lines from one file and save into another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pulling x number of lines from one file and save into another
# 1  
Old 04-20-2011
Pulling x number of lines from one file and save into another

Hi,

I have a log file (updates.log), and I want to hunt the file for any errors. Here's an example of the log file:

Code:
SQL> update <table1> set <value1> = '*****';
update <table1> set <value1> = '*****'
       *
ERROR at line 1:
ORA-00942: table or view does not exist


Elapsed: 00:00:00.00
SQL> update <table2> set <value2> = '*****';
update <table2> set <value2> = '*****'
       *
ERROR at line 1:
ORA-00942: table or view does not exist


Elapsed: 00:00:00.00

I'd like to be able to pull out the 8 rows from SQL> to Elapsed, but not sure how to do this. ATM I'm grepping the file looking for ERROR, but the result that I'm given is more or less useless (ERROR at line 1). I only want to pull out these 8 lines if there's an ERROR.

Any idea how I can acheive this? Have taken a look through this forum but haven't managed to find a solution to fit this - any feedback/ thoughts appreciated.

Cheers
# 2  
Old 04-20-2011
Try:
Code:
awk '/^SQL/{s=$0;next}{s=s RS $0} /^ERROR/{f=1} f && /^Elapsed/{print s; f=0}' file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 04-20-2011
Hi

I added the above to my script, however it didn't do anything (i.e. no ERRORS within my new log file, however there are errors in update.logs).

The script doesn't error, just hangs at command line - your scrpt is the last bit of code within my script Smilie
# 4  
Old 04-20-2011
Code:
#!/bin/bash
#tested with bash 4

content=$(<file)
while [[ $content =~ Elapsed ]]
do
    c=${content#*Elapsed}
    case "${content%%Elapsed*}" in
        *SQL*ERROR*)
        echo "${content%%Elapsed*}"
        ;;
    esac
    content="$c"
done

This User Gave Thanks to bash-o-logist For This Post:
# 5  
Old 04-20-2011
Quote:
Originally Posted by dbchud
Hi

I added the above to my script, however it didn't do anything (i.e. no ERRORS within my new log file, however there are errors in update.logs).

The script doesn't error, just hangs at command line - your scrpt is the last bit of code within my script Smilie
With an example of your input file I get this output:

Code:
$ cat file
Other line
Other line
Other line
SQL> update <table1> set <value1> = '*****';
update <table1> set <value1> = '*****'
       *
ERROR at line 1:  FIRST ERROR
ORA-00942: table or view does not exist


Elapsed: 00:00:00.00
Other line
Other line
Other line
SQL> update <table2> set <value2> = '*****';
update <table2> set <value2> = '*****'
       *
ERROR at line 1: SECOND ERROR
ORA-00942: table or view does not exist


Elapsed: 00:00:00.00
Other line
Other line
Other line
$ 
$ awk '/^SQL/{s=$0;next}{s=s RS $0} /^ERROR/{f=1} f && /^Elapsed/{print s; f=0}' file
SQL> update <table1> set <value1> = '*****';
update <table1> set <value1> = '*****'
       *
ERROR at line 1: FIRST ERROR
ORA-00942: table or view does not exist


Elapsed: 00:00:00.00
SQL> update <table2> set <value2> = '*****';
update <table2> set <value2> = '*****'
       *
ERROR at line 1: SECOND ERROR
ORA-00942: table or view does not exist


Elapsed: 00:00:00.00
$

This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Number of Lines in a file

Hi All, This is my Scenario: I wanted to check if a particular name or pattern is present in a file based of that rest of the program should proceed. I want to print '0' if no matching found. v_File_Count=`grep -i "$v_Name_Pattern" $File_Path/Master_File_List.txt | wc -l` The above command... (5 Replies)
Discussion started by: TechGyaann
5 Replies

2. Shell Programming and Scripting

Remove duplicate lines, sort it and save it as file itself

Hi, all I have a csv file that I would like to remove duplicate lines based on 1st field and sort them by the 1st field. If there are more than 1 line which is same on the 1st field, I want to keep the first line of them and remove the rest. I think I have to use uniq or something, but I still... (8 Replies)
Discussion started by: refrain
8 Replies

3. Shell Programming and Scripting

Number of lines in file

How can I find the number of lines in a file excluding lines starting with #? (4 Replies)
Discussion started by: kristinu
4 Replies

4. Shell Programming and Scripting

How to remove blank lines in a file and save the file with same name?

I have a text file which has blank lines. I want them to be removed before upload it to DB using SQL *Loader. Below is the command line, i use to remove blank lines. sed '/^ *$/d' /loc/test.txt If i use the below command to replace the file after removing the blank lines, it replace the... (6 Replies)
Discussion started by: vel4ever
6 Replies

5. Shell Programming and Scripting

cut lines from log file and save it another file

Dears, i want cut the lines from a log file. Example of the log file as follows.. May 27, 2011 5:54:51 PM com.huawei.ivas.utilities.sm.client.SMDeliverContrUtil isDeliverSM FINE: May 27, 2011 5:54:51 PM com.huawei.ivas.utilities.sm.client.SMUtil addSysUpMsgLog INFO: . The message content... (1 Reply)
Discussion started by: tonypalokkaran
1 Replies

6. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

7. Shell Programming and Scripting

Grep multiple lines and save to a file

Sir I have a data file e.g. DATA31082009. This file consists of several data files appended to that file. The size of each data file is different. The first line of each file starts with "44". I want to grep data from "44" to the preceding line of next "44" and save it as a individual file.... (10 Replies)
Discussion started by: chssastry
10 Replies

8. Shell Programming and Scripting

Pulling the first and last character/number from a string.

Let's say I have a word "foobar23" in a file, and I want to pull the first "f" and last "3" character out of the world, how would I accomplish that? # cat file foobar23 I want the output to be: f3 (3 Replies)
Discussion started by: LinuxRacr
3 Replies

9. Shell Programming and Scripting

Pulling multiple lines of text

Hello, So, I'm not even sure if this will be possible for me to do (then again, that's why I'm asking for help ;) ) What I'm trying to do is pull multiple lines out of a very large text file, separating them into smaller files. Basically it's a text archive of a few hundred emails. What I... (2 Replies)
Discussion started by: DumDum
2 Replies

10. Shell Programming and Scripting

Pulling data and following lines from file

I saw a few posts close to what i want to do, but they didn't look like they would work exactly.. or I need to think out of the box on this. I have a file that I keep server stats in for my own performance analysis. this file has the output from many commands in it (uptime, vmstats, ps, swap... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question