How to fetch specific data from a file.?

Hi ,

I have a file which contains 2 days logs(here it is 24 and 25)
I want to list data only for date 25 fron the file.
please suggest me how should i get this.

file content mentioned below

17-05-24  Name                                                  Succ    Fail
         
00:00:29  Received                                                153      0
          Rejected                                                 0       0
        
00:00:39  Received                                                148      0
          Rejected                                                 0       0
17-05-25  Name                                                  Succ    Fail
                                                                            
00:00:49  Received                                                134      0
          Rejected                                                 0       0
          
00:00:59  Received                                                137      0
          Rejected                                                 0       0
 
 Expected output
 17-05-25  Name                                                  Succ    Fail
                                                                            
00:00:49  Received                                                134      0
          Rejected                                                 0       0
          
00:00:59  Received                                                137      0
          Rejected                                                 0       0
          
 
#based only on your data sample - one way to do it

awk ' BEGIN{ok=0}
     {
        if($2 == "Name")
        {
            if($1=="17-05-25")
            {
                ok=1
            }
            else
            {
                 ok=0
            }
        }         

        ok==1 {print}
 
    } ' infile > outfile

I deliberately did not use more succinct ways of doing this so you can easily see the logic. The flag can change only when the input line has the string "Name" in it.

hi jim

not able to understand below line.

 
  
 awk ' BEGIN{ok=0}
 

what is ok =0 means here

---------- Post updated at 12:04 PM ---------- Previous update was at 11:26 AM ----------

hi jim,

also getting error while running it .

 
 #!/bin/bash
DT= `date +%y-%m-%d` 
cat vsy.txt |awk ' BEGIN{ok=0}{if($2 == "Name"){if($1=="$DT"){ok=1}else{ok=0}}ok==1 {print}} ' infile > outfile
 

error

 
 # ./v.sh 
./v.sh: line 2: 17-05-29: command not found
awk:  BEGIN{ok=0}{if($2 == "Name"){if($1=="$DT"){ok=1}else{ok=0}}ok==1 {print}} 
awk:                                                                   ^ syntax error
 

May I jump in?

It explicitely defines the ok variable to 0 before any input is read (which wouldn't be necessary as awk allocates variables on first reference and initializes to zero or empty)

Please DON'T modify proposals given unless you exactly know what you are doing. E.g. you can't use shell variables ( DT ) inside awk scripts. Use e.g. -vDT="$DT" (or other mechanisms). And, you supply TWO input streams to awk : one from the - useless - cat pipe, and one from "infile" (which I presume doesn't exist on your system and thus is empty). awk uses the latter if provided. And, don't condense multiline scripts into one-liners without taking care of the necessary adaptions.
That said, jim mcnamara's proposal actually has a problem. Try this corrected version:

awk '
BEGIN   {ok = 0
        }
        {if ($2 == "Name")      {if ($1=="17-05-25")    {ok = 1
                                                        }
                                 else                   {ok = 0
                                                        }
                                }
        }
ok == 1 {print
        }
' file

Run as given first, check for errors, and then only try to adapt to your needs.

hi rudic

it works but how should I incorporate in script.

I am explaining my requirement as below.

 
 i have a file called vsy.txt.0 and vsy.txt.1
i need the data of day -1 but the day -1 data is present in 2 files which is vsy.txt.0 and vsy.txt.1
also file is getting updated at every 10 sec.
 what i need here is to sum the received count for 1 hr and save in a file.
in this way i can have a 24 entry on houry basis for received message . 

 

---------- Post updated at 05:50 PM ---------- Previous update was at 01:04 PM ----------

please help me in this

It is hard to follow what you are doing. You say that you have one file with two names ( vsy.txt.0 and vsy.txt.1 ) and that this file is updated every 10 seconds.

Is one of these files a symbolic link pointing to the other file? Are both files hard linked? If it is one file, why does it matter to your script which name is used?

How is the file being updated? Are the contents overwritten every 10 seconds? Is text appended to your file every 10 seconds?

If you just want a count of records added every hour and text is being appended to your file, why not just wait until the next day after all of the updates have been received and process your file once?

If you don't give us a clear description of what your computing environment is, what input file(s) you have, the format of that file (or those files) is, the format of the output you want, and sample input files and corresponding sample output that want to extract that the sample input, there is little incentive for the volunteers reading your post to try to guess at what you are trying to do and guess at something the stands very little chance of actually meeting your unspecified requirements.

With what you have told us, why doesn't running the code RudiC suggested every 10 seconds after your file has been updated give the the results you want until the day is done?