Need to check a file from a certain position and date onwards

Hi Guys,
I need some advice please.

My script is not grabbing information from a text file from a certain date correctly. It seems to be grabbing everying in the file, i know it is something simple but i have looked to hard and to long, to know what the issue is.

Script

 awk ' BEGIN {ok=0}
        substr($(NF),1,2) >= 11 {ok=1}
        ok==0 {next}
        {print} ' DATE_1=`date +%m/%d/%y` /tmp/test   | grep "Set owner " >> $TMP2/OWNERSHIP_ASSIGNED_INJECTED_$DATE.txt

Script Ouptut

+ awk  BEGIN {ok=0}
        substr($(NF),1,2) >= 11 {ok=1}
        ok==0 {next}
        {print}  DATE_1=04/13/12 /tmp/test

File Output - OWNERSHIP_ASSIGNED_INJECTED_130412.txt

Set owner nbkpmstr to CBLJMM   -  Should not be shown, yesterday file input!!
Set owner nbkpmstr to CBL288
Set owner nbkpmstr to CBL011
Set owner nbkpmstr to CBL225
Set owner nbkpmstr to CBL132

File Read - /tmp/test


$ cat /tmp/test
04/12/12 15:10:25
volume: CBLJMM
Remove prior owner SYSTEM from CBLJMM
Set owner nbkpmstr to CBLJMM

04/13/12 15:10:14
volume: CBL288
Remove prior owner SYSTEM from CBL288
Set owner nbkpmstr to CBL288

04/13/12 15:10:25
volume: CBL011
Remove prior owner SYSTEM from CBL011
Set owner nbkpmstr to CBL011

04/13/12 15:10:36
volume: CBL225
Remove prior owner SYSTEM from CBL225
Set owner nbkpmstr to CBL225

04/13/12 15:10:37
volume: CBL132
Remove prior owner SYSTEM from CBL132
Set owner nbkpmstr to CBL132

please post what is the output you are expecting from the above input?

We should only see out for the currernt date i.e 04/13/2012

Set owner nbkpmstr to CBL288
Set owner nbkpmstr to CBL011
Set owner nbkpmstr to CBL225
Set owner nbkpmstr to CBL132

 
xargs -l4 <  input_file | grep `date "+%m/%d/%y"` | sed 's/.*Set/Set/'

panyam,

Can you explain to me how i should enter this into the script.

xargs -l4 <  input_file | grep `date "+%m/%d/%y"` | sed 's/.*Set/Set/'

The script should read the file below looking for the current date and grab only "Set owner ". So from the example below is should grab CBL288 and not CBLJMM..

File Read - /tmp/test

$ cat /tmp/test
04/12/12 15:10:25
volume: CBLJMM
Remove prior owner SYSTEM from CBLJMM
Set owner nbkpmstr to CBLJMM

04/13/12 15:10:14
volume: CBL288
Remove prior owner SYSTEM from CBL288
Set owner nbkpmstr to CBL288

substr($(NF),1,2) >= 11 {ok=1}

What is this trying to check? You're also not using DATE_1.

An awk solution could be:

awk '$1 ~ DATE1 {ok=1} ok==0 {next} /Set/ {print}' DATE1=$(date +%m/%d/%y) /tmp/test

Depending on your OS, grep may offer a 'by paragraph' or after context option you could use. e.g. with GNU grep:

export DATE_1=$(date +%m/%d/%y); grep -A4 "$DATE_1" test | grep Set

Panyam

Your syntax is working, but its showing more than i require.

Set owner nbkpmstr to CBH366 04/13/12 15:16:28 volume: CBH366 Remove prior owner SYSTEM from CBH366

I only want to see

Set owner nbkpmstr to CBH366 

How can i update the code to do this please

xargs -l4 <  input_file | grep `date "+%m/%d/%y"` | sed 's/.*Set/Set/'

Hello,

The code I suggested should work provided the input format of your file does not change!!!

Tested in my system as below:

 
SCRIPTS>cat input_file
04/12/12 15:10:25
volume: CBLJMM
Remove prior owner SYSTEM from CBLJMM
Set owner nbkpmstr to CBLJMM
04/13/12 15:10:14
volume: CBL288
Remove prior owner SYSTEM from CBL288
Set owner nbkpmstr to CBL288
04/13/12 15:10:25
volume: CBL011
Remove prior owner SYSTEM from CBL011
Set owner nbkpmstr to CBL011
04/13/12 15:10:36
volume: CBL225
Remove prior owner SYSTEM from CBL225
Set owner nbkpmstr to CBL225
04/13/12 15:10:37
volume: CBL132
Remove prior owner SYSTEM from CBL132
Set owner nbkpmstr to CBL132

SCRIPTS>xargs -l4 <  input_file | grep `date "+%m/%d/%y"` | sed 's/.*Set/Set/'
Set owner nbkpmstr to CBL288
Set owner nbkpmstr to CBL011
Set owner nbkpmstr to CBL225
Set owner nbkpmstr to CBL132

Regards
Ravi

1 Like

All 3 solutions work for me on bash/RHEL 5.7. What's your OS?

EDIT: The xargs and awk versions work on Solaris 9 as well.

I am using KSH

---------- Post updated at 02:21 PM ---------- Previous update was at 02:09 PM ----------

I have put in a workaround, as i am using KSH in Solaris.