Output section of file between two expressions multiple times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output section of file between two expressions multiple times
# 1  
Old 06-10-2010
Output section of file between two expressions multiple times

Attached is the exact ouput of a vmware VDR log file I am working with but what I am trying to achieve is as follows:

I need to output sections of the file using the string "Normal backup" as the start and "Duration" as the end to seperate files so I can then manipulate them further to create seperate backup logs I can then email.

I found an awk one liner which does what I need but I cannot think how to extract each section to seperate log files. I'm finding this hard to explain so here is an example.

Code:
awk '/Normal backup/,/Duration/' /tmp/backuplog.txt

So what I need is for each section it matches to then output to a seperate file (doesn't have to be awk obviously), e.g

Code:
magicshellcode > /tmp/logfile1.txt, /tmp/logfile2.txt, /tmp/logfile3.txt

etc, etc,

From there I am going to run some more shell stuff over them to tidy them up which i should be able to handle myself.

I apologise if this is hard to understand but I found it difficult to communicate without having a script to begin with.
# 2  
Old 06-10-2010
Try this:
Code:
awk '
$4 == "Normal" && $5 == "backup"{f=1; c++}
f{print > "/tmp/logfile" c ".txt"}
$4 ~ /Duration/{f=0}
' /tmp/backuplog.txt

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 06-10-2010
Quote:
Originally Posted by Franklin52
Try this:
Code:
awk '
$4 == "Normal" && $5 == "backup"{f=1; c++}
f{print > "/tmp/logfile" c ".txt"}
$4 ~ /Duration/{f=0}
' /tmp/backuplog.txt

Wow, Thanks! I am stoked with that response since it works perfectly!! Exactly what I was after
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace string and create new file multiple times

First of all, apologies if this has already been answered elsewhere. I haven't quite been able to find what I'm looking for yet, so hopefully this won't come across as repetition. I have a file consisting of ~100 nearly identical lines, each of which contains multiple instances of the string I... (11 Replies)
Discussion started by: pseudo.seppuku
11 Replies

2. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

3. UNIX for Dummies Questions & Answers

How to check if the same file exists multiple times?

Hi Team , Is there a way I can check to see if the same file say , test.dat exists multiple times in the directory path ? Please help. Thanks Megha (5 Replies)
Discussion started by: megha2525
5 Replies

4. UNIX for Dummies Questions & Answers

copying same file multiple times with different names

hi, I am copying a file from 1 folder to another in /bin/sh. if the file already exists there, it should get copied as filename1. again if copying next time it shouldget copied as filename2.. , filename3..so on.. The problem is i am able to get uptil filename1.. but how do i know what... (6 Replies)
Discussion started by: blackcat
6 Replies

5. Shell Programming and Scripting

Format output from the file to extract "date" section

Hello Team , I have to extract date section from the below file output. The output of the file is as shown below. I have to extract the "" this section from the above output of the file. can anyone please let me know how can we acheive this? (4 Replies)
Discussion started by: coolguyamy
4 Replies

6. 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

7. Shell Programming and Scripting

Append some text to a file multiple times

Hi, I have a text file like Version=abc Tab=1 URL GOTO=www.abc.com/board=1 some text... I want to run a loop x no of times and append to the text file above text but URL GOTO should be www.abc.com/board=2 then 3,4...etc till x. Kindly help (2 Replies)
Discussion started by: krabu
2 Replies

8. Shell Programming and Scripting

call a passwd file to a script multiple times

Hello everybody, I have a requirement in my script.. When i'am executing a script, it'll ask a passwd of some service account.. I need to pass it to the script through a zipped file when it asks for it. The script can be executed by more people many number times. So for securty purpose, it... (1 Reply)
Discussion started by: raghu.iv85
1 Replies

9. Shell Programming and Scripting

Will the output file be opened and closed several times?

Hi, there, I wrote a script like this: #!/bin/bash #put something into a LIST for item in $LIST do cat $item >> /tmp/output done My question is that if I have 5 items in that LIST, should it be opened and closed every time when the ">>" works? So that file will be opened and... (7 Replies)
Discussion started by: koifans
7 Replies

10. Shell Programming and Scripting

Prevent file from being mailed multiple times from a job

We have a ksh which runs once every 15 minutes. Based on a certain condition (for invalid data) we are spooling a file and if the file is of length greater than 0 bytes, then we are mailing this file to a group of users. Upon receiving the file, users correct the data so that on its next run the... (2 Replies)
Discussion started by: Sree_2503
2 Replies
Login or Register to Ask a Question