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

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

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

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:

grep , logfile
awk -v P1="project.*3" -v P2="project.*4" '$0~P1{f=1;next}$0~P2{f=0;next}f' file

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

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

'$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

Here is the explanation of 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.

$ cat temp.x
project1
project2
project3

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

project4
project5
$ 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:

sed -n "/project3/,/project4/ p" temp.x | grep -v "^project"

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:

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.