Need help to pick the content from Log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to pick the content from Log file
# 1  
Old 03-20-2013
Need help to pick the content from Log file

Hi All,

Below is my requirement.

I need to pick the certain content from log file and display. for example log file consist of following information

HTML Code:
project1
project2
project3

filename1, size1, ower1, datecreated
filename2, size2, ower2, datecreated
filename3, size3, ower3, datecreated
filename4, size4, ower4, datecreated

project 4
project 5
Is there any way to pick those
HTML Code:
filename1, size1, ower1, datecreated
filename2, size2, ower2, datecreated
filename3, size3, ower3, datecreated
filename4, size4, ower4, datecreated
details

if we pass search strings project3 and project4 as inbound and outbound as search parameters

Thanks,
chandu
# 2  
Old 03-20-2013
We need a much better description of what you're trying to do.

For the data given, the following will do what you have requested:
Code:
grep , logfile

# 3  
Old 03-20-2013
Code:
awk -v P1="project.*3" -v P2="project.*4" '$0~P1{f=1;next}$0~P2{f=0;next}f' file

# 4  
Old 03-20-2013
Hi Don,

I need to fetch the data between certain limits. In the above example if we pass the project3,project4 as arguments, I need the contents between those 2 arguments.

In between project3 & project4 below contents are there

Code:
filename1, size1, ower1, datecreated
filename2, size2, ower2, datecreated
filename3, size3, ower3, datecreated
filename4, size4, ower4, datecreated

I want to select that part.

---------- Post updated at 01:41 AM ---------- Previous update was at 01:36 AM ----------

Thanks Yoda..

---------- Post updated at 02:22 AM ---------- Previous update was at 01:41 AM ----------

Yoda,

Can you explain me this part

Code:
'$0~P1{f=1;next}$0~P2{f=0;next}f'

I didn't get what exactly its performing. I got the desired result but I dont want to use this blindly.

Thanks,
Chandu
# 5  
Old 03-20-2013
Here is the explanation of code:
Code:
        $0 ~ P1 {               # Check if current record matches pattern: P1 (project.*3)
                f = 1           # If yes, then set flag variable f = 1
                next            # Skip processing current record
        }

        $0 ~ P2 {               # Check if current record matches pattern: P2 (project.*4)
                f = 0           # If yes, then set flag variable f = 0
                next            # Skip processing current record
        }

        f                       # If f == 1 (true) AWK's default operation is to print current record

Basically we are setting a flag variable f to 1 when first pattern is matched & we set it to 0 when second is matched, the records are printed only if the flag is set to 1.
# 6  
Old 03-20-2013
Code:
$ cat temp.x
project1
project2
project3

filename1, size1, ower1, datecreated
filename2, size2, ower2, datecreated
filename3, size3, ower3, datecreated
filename4, size4, ower4, datecreated

project4
project5

Code:
$ sed -n "/project3/ {:mark n; /project4/q; p; b mark}" temp.x

filename1, size1, ower1, datecreated
filename2, size2, ower2, datecreated
filename3, size3, ower3, datecreated
filename4, size4, ower4, datecreated

Or, more simply:
Code:
sed -n "/project3/,/project4/ p" temp.x | grep -v "^project"

# 7  
Old 03-20-2013
The code hanson44 provided didn't notice that the original input had project3 and project 4 with no indication of whether or not searching for lines in other cases will or will not have one or more spaces in the middle of the project name.

The code Yoda provided assumes that there won't be multiple matching project names. (For example, if projects 1 through 40 are in your input file with or without spaces in their names, Yoda's code will print data between the lines starting project3 and project4, project 13 and project 14, project 23 and project 24, and project 30 and project 34.)

You might want to try something like this instead:
Code:
awk -v P1="3" -v P2="4" '$0~"project *"P1"$" {f=1;next}$0~"project *"P2"$"{f=0;exit}f' file

It follows the logic used by Yoda, but searches for lines identifying projects based on matching the entire line against "project" followed by zero or more spaces, followed by the project number occurring at the end of an input line. It exits when it finds the ending project instead of reading the rest of the input file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX Log File Content - Duplication Issue

I have a shell script with 2 run time arguments. During the execution if i got any error, then it needs to redirected to a error file and in console. Also both error and output to be redirected to a log file. Error output is getting copied to err file and getting displayed in console too. But... (2 Replies)
Discussion started by: sarathy_a35
2 Replies

2. Shell Programming and Scripting

Check specific content from log file

Hi all, i have a logfile which is continuously being updated. I have built a script to check for a specific content and if it is found, it sends a string into a file. Here's the current script: #!/bin/bash logfile=/opt/jboss-eap-6.3/standalone/log/server.log tail -fn0 $logfile | \... (7 Replies)
Discussion started by: nms
7 Replies

3. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

4. Shell Programming and Scripting

Pick the last one hour lines from log matching this pattern.

Hello please help me on this, pick the last one hour lines from the log, which have the prefix time format like this. log message log message i tried to do grep, but that failed. my code grep '(date +)' log_file_path This checking only the current time stamp. How to get the log... (16 Replies)
Discussion started by: santosh2626
16 Replies

5. Shell Programming and Scripting

Create archive and nil the content of log file using script

Plese help I need a urgent requirement. Ex: test.log requirement : using shell script I need to archive the log file and nil and the content of (test.log) file to 0 kb and then in the archive folder log files are name to test.tar test1.tar test2.tar EX: /home/abc/ test.log ... (1 Reply)
Discussion started by: johney1981
1 Replies

6. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

7. Shell Programming and Scripting

Grep the Content of a LOG File which has latest Date and Time

Hi All, Need a small help. I have a log file which keeps updating for every Minute with multiple number of lines. I just want to grep few properties which has latest Date and Time to it. How do i do it? I wanted to grep a property by name "Reloading cache with a maximum of" from the... (4 Replies)
Discussion started by: nvindraneel
4 Replies

8. Shell Programming and Scripting

Pick one file from each subdirectory

Hi, I have a problem I am trying to solve with bash. I need to search in a file system (data base) with hundreds of directories and thousands of subdirectories and millions of files. The files have a specific format with a header that gives the properties. Directories are organized so... (1 Reply)
Discussion started by: majest
1 Replies

9. Shell Programming and Scripting

Pick out the various extension file from log of the same date

extension file from log (3 Replies)
Discussion started by: NehaKrish
3 Replies

10. Shell Programming and Scripting

how can i pick the latest log file as per below

in the below .. i want to pick the latest logfile which is having JPS.PR inside.. that means i want particularly "spgport040408041223.log:@@@@@@@@ 04:13:09 Adding: JPS.PR." which is latest among these.. is it possible to compare the current time with logfile time ? reptm@xblr0758rop>... (4 Replies)
Discussion started by: mail2sant
4 Replies
Login or Register to Ask a Question