Sponsored Content
Top Forums UNIX for Beginners Questions & Answers How to fetch specific data from a file.? Post 302998278 by RudiC on Monday 29th of May 2017 03:03:14 AM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 08:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy