Copy/print all lines between pattern is found in .log files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy/print all lines between pattern is found in .log files
# 1  
Old 11-20-2012
Copy/print all lines between pattern is found in .log files

Hi,

I have a folder with multiple (< 33) .log files.
And I have to copy the lines between two patterns from all the .log files to a new file.

(script file with a loop?)

Thanks in advance.


1.log
Code:
Code:
...
..
xx1> begin
...
..
..
>>> Total: 2 Alarms
..
..


2.log
Code:
Code:
...
..
xx2> begin
...
..
..
>>> Total: 2 Alarms
..
..


Last edited by AK47; 11-20-2012 at 03:19 PM..
# 2  
Old 11-20-2012
Code:
$ cat file1
...
..
xx1> begin
...
..
..
>>> Total: 1 Alarms
..
..
...
..
xx2> begin
...
..
..
>>> Total: 2 Alarms
..
..
$ cat file2
...
..
xx3> begin
...
..
..
>>> Total: 3 Alarms
..
..
...
..
xx4> begin
...
..
..
>>> Total: 4 Alarms
..
..
$ sed '/begin/,/Total/!d' file[12]
xx1> begin
...
..
..
>>> Total: 1 Alarms
xx2> begin
...
..
..
>>> Total: 2 Alarms
xx3> begin
...
..
..
>>> Total: 3 Alarms
xx4> begin
...
..
..
>>> Total: 4 Alarms

If you have too many logfiles, then "*.log" could be problematic, and you should look at using a loop of some kind.
# 3  
Old 11-20-2012
Yes, I will have to use a shell file for loop

I have tried the script, but is stay stock.
# 4  
Old 11-20-2012
Why?

And by "stock", you mean "stuck"?

Post what you have. And say why you "have to" use a loop.
# 5  
Old 11-20-2012
it stays stuck. (typo :s )

Code:
sed '/begin/,/Total/!d 001.log

Because it has to do it for all 35 .log files in the folder and output it in a new file Smilie

Last edited by Scott; 11-20-2012 at 03:41 PM.. Reason: Code tags
# 6  
Old 11-20-2012
You missed a closing ' (single quote).

To do all 35 files you need to replace "001.log" with a filespec that matches all of the files (i.e. *.log).

And please don't edit your previous posts in a thread after it has been replied to. This only serves to confuse people who haven't already read it.
# 7  
Old 11-20-2012
Code:
#!/bin/ksh
###finding pattern in multiple log files
ll -rt *.log | tr -s "[:space]" " " | cut -d" " -f9 > Logfiles2check
rm -f OutputFile.txt
for each_file in `cat Logfiles2check`
do
awk '/begin/,/Total:/' ${each_file} >> OutputFile.txt
echo "\n\n" >> OutputFile.txt
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

3. Shell Programming and Scripting

Print n lines from top and n lines from bottom of all files with .log extenstion

Oracle Linux 6.4 In a directory I have more than 300 files with the extension .log I want the first 5 and last 5 lines of these .log files to be printed on screen with each file's name. Expected output : Printing first 5 and last 5 lines of FX_WT_Feb8_2014.log !! Authentication... (7 Replies)
Discussion started by: kraljic
7 Replies

4. Shell Programming and Scripting

Print character after pattern found

Hi Gurus, i need your help to create a script the will print a characters after the pattern was found. Sample lines are below: My birthday:"1977-16-07", My birthday:"1975-16-07" My birthday:"1970-16-07". My patter should be "birthday:", then i want to print the following characters which... (18 Replies)
Discussion started by: scripter123
18 Replies

5. Shell Programming and Scripting

Print first 20 lines from all .log files

RHEL 5.8 In a directory I have more than 200 files with the extension .log Using head command I want to print first 20 lines of each log file. For each .log file, it should print like below Printing first 20 lines of : GRP_error_April29.log Apr 29 04:02:05 raptor03b syslogd 1.4.1:... (4 Replies)
Discussion started by: John K
4 Replies

6. Shell Programming and Scripting

awk to print all lines after a pattern is found

Is there a way with aw to print all lines after a string is found There is a file like this ....... ........ 2012/19/11 :11.58 PM some data lne no date 2012/19/11 :11.59 PM some other data 2012/20/11 :12.00 AM some other data some line without dates some more lines without dates... (8 Replies)
Discussion started by: swayam123
8 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

print next word after found pattern

Hi all, I'd like to print the next word after a found pattern. example text: word1 word2 word3 word4 pattern word5 pattern word1 word2 word3 word4 word1 word2 pattern word4 basiclly the word after pattern. Thanks (9 Replies)
Discussion started by: stinkefisch
9 Replies

9. Shell Programming and Scripting

copy lines from log files based on timestamp and sysdate

I'm looking for a command or simple script that will read lots of audit log file (*.aud) in log fold every 10 minutes, and will output to one file based on sysdate - 10 minutes. assume the script is run at 11:12:20, and it should grep the line from Wed Jun 17 11:02:43 2009 to end of file. after... (4 Replies)
Discussion started by: percvs88
4 Replies

10. Shell Programming and Scripting

copy lines from log files based on timestamp and sysdate

I am sorry to repost this question. it was not clear, and I had the meeting and didn't response the question on time. I do really need help and appreciate your help very much. I'm looking for a simple shell script that will read lots of audit log file (*.aud) in a log fold every 10 minutes,... (1 Reply)
Discussion started by: percvs88
1 Replies
Login or Register to Ask a Question