How to fetch specific data from a file.?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to fetch specific data from a file.?
# 1  
Old 05-26-2017
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
Code:
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

Code:
 
 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

# 2  
Old 05-26-2017
Code:
#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.
# 3  
Old 05-29-2017
hi jim

not able to understand below line.
Code:
 
  
 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 .
Code:
 
 #!/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
Code:
 
 # ./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

# 4  
Old 05-29-2017
May I jump in?
Quote:
Originally Posted by scriptor
hi jim

not able to understand below line.
Code:
 
  
 awk ' BEGIN{ok=0}

what is ok =0 means here
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)

Quote:
hi jim,

also getting error while running it .
Code:
 
 #!/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
Code:
 
 # ./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

Please DON'T modify proposals given unless you exactly know what you are doing. E.g. you can't use shell variables (DT) inside awkscripts. 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:
Code:
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.

Last edited by RudiC; 05-29-2017 at 04:08 AM..
# 5  
Old 05-29-2017
hi rudic

it works but how should I incorporate in script.

I am explaining my requirement as below.

Code:
 
 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
# 6  
Old 05-30-2017
Quote:
Originally Posted by scriptor
hi rudic

it works but how should I incorporate in script.

I am explaining my requirement as below.

Code:
 
 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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetch data from file

Hi, I am new to scripting. I have a log file and need to fetch specific logs and copy to another file. A copy of the log is like this: =============================================================== = JOB : server123#jobs1.jobstream1 = USER : andyc = Tue 08/01/17... (3 Replies)
Discussion started by: Prngp
3 Replies

2. Shell Programming and Scripting

Fetch Data from File using UNIX or Perl

Hello, How All are Doing today. I have a issue, I have a file which contains the data as follow <ENVELOPE><ENVELOPE_ID>TEST</ENVELOPE_ID><ENVELOPE_EXTERNAL_ID></ENVELOPE_EXTERNAL_ID><ENVELOPE_VERSION>2</ENVELOPE_VERSION><SIResourceDefaultVersion>true</SIResourceDefaultVersion><TYPE>GS... (1 Reply)
Discussion started by: adisky123
1 Replies

3. Shell Programming and Scripting

Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as (7 Replies)
Discussion started by: manigrover
7 Replies

4. Shell Programming and Scripting

Urgent request to consider:Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as # Drug_Target_X_Gene_Name:(Where X can be any number (1-1000) (1 Reply)
Discussion started by: manigrover
1 Replies

5. Shell Programming and Scripting

Fetch data between two dates from a file

Hi All, I m new to this forum & UNix too. currently i have a requirement which can fetch data from a logfile between two dates or timestamp. for example: 1. data from 2012 Jun to 2012 Jul 2. data from 2012 Jun to 2012 Jul 07 3. data from 2012 Jun 16 10:20 to 2012 Jul 03 10:10 Please... (7 Replies)
Discussion started by: KDMishra
7 Replies

6. Shell Programming and Scripting

How to fetch data between two timestamps in a file using KSH

Hi, I got a requirement to fetch data between two time stamps in a big log file and grep for a word in that particular time interval of data. Here is my log looks like: 2012/04/08-14:35:56 Abcdefg 2012/04/08-14:35:56 Hijklmnophhoishfw 2012/04/08-14:35:56... (1 Reply)
Discussion started by: siri_886
1 Replies

7. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

8. Shell Programming and Scripting

How to fetch a specific line from file

Hi, I have text file in the following strucher . The files contain hondreds of lines. value1;value2;value3;value4 I would like to get back the line with lowest date (values4 field). In this case its line number 3. groupa;Listener;1;20110120162018 groupb;Database;0;20110201122641... (4 Replies)
Discussion started by: yoavbe
4 Replies

9. Shell Programming and Scripting

To fetch specific words from a file

Hi All, I have a file like this,(This is a sql output file) cat query_file 200000029 12345 10001 0.2 0 I want to fetch the values 200000029,10001,0.2 .I tried using the below code but i could get... (2 Replies)
Discussion started by: girish.raos
2 Replies

10. Shell Programming and Scripting

How to fetch data from a text file in Unix

I want to fetch passwords from a common file xxxx.txt and use it in a script. Currently the password is hardcoded so this have to be changed so that password can be fetched from text file..... Please reply asap.. Thanks (4 Replies)
Discussion started by: shikhakaul
4 Replies
Login or Register to Ask a Question