Operating System - HP-UX
1752579 Members
4059 Online
108788 Solutions
New Discussion юеВ

Re: Read text file from a specified string to the end of the file.

 
bsraj
Occasional Advisor

Read text file from a specified string to the end of the file.

Hi All,

I like to read the log file from specific time to end of the file.

eg:
message0
date and time0
message 1
date and time1
message 2
EOF

I want to read all the text (Messages) after date and time0 to end of the file.
Please let me know the UNIX command to perform this?.

Thanks,
bsraj.
15 REPLIES 15
VK2COT
Honored Contributor

Re: Read text file from a specified string to the end of the file.

Hello,

It can be done in many, many ways (Sed, Perl,
Awk, and so on). I am sure our colleagues
in the Forum will flood you with responses :)

Since I am on holidays (sigh, why am I
then still logging infor HP Forums :)),
here are two simple methods in Awk (I am not
sure which version of Awk you use):

awk '/date and time0/,EOF {getline;print}'

or

awk /date and time0/,0 {getline;print}'

Cheers from sunny and hot Sydney - Australia,

VK2COT
VK2COT - Dusan Baljevic
bsraj
Occasional Advisor

Re: Read text file from a specified string to the end of the file.

Hi,

Thanks for your response.
I am trying to read the log file (from the specified data and time ) using my K-Shell script and i am creating a new temp file with only the contents after the specified date and time.

Thanks,
bsraj.
VK2COT
Honored Contributor

Re: Read text file from a specified string to the end of the file.

Hello,

That is fine.

You simply put the folloiwiung line in your Ksh script. Sometjhing along the followiung line:

#!/bin/ksh

PATH=/usr/bin:/sbin:/bin
export PATH

INPUTLOG="/somedir/mylogfile.txt"
OUTFILE="someotherdir/results.txt"

if [ -s "$INPUTFILE" ]
then
awk '/date and time0/,EOF {getline;print}' $INPUTFILE >$OUTFILE
if [ $? -ne 0 ]
then
echo "WARNING: Command awk failed"
exit 1
fi
fi
exit 0

If you are happy with the solution please do
not forget to assign points before closing the thread :)

Cheers,

VK2COT

VK2COT - Dusan Baljevic
bsraj
Occasional Advisor

Re: Read text file from a specified string to the end of the file.

Hi VK2COT,

I appreciate your help, but again I am having some issues.
I am getting the following error
awk: syntax error near line 1
awk: bailing out near 1
and I am not sure this is because of the spaces in between the date and time.

Please find my sample code.

#! /bin/ksh
set -x

LogFile="/tmp/scan_log.log"
LoadFile="/tmp/new_scan_log.log"
Last_Alert="Alert: Fri Jan 4 16:15:01 2008"

if [ -s "$LogFile" ]
then
awk '/$Last_Alert/,EOF {getline;print}' $LogFile >> $LoadFile
fi
exit 0


Thanks,
bsraj
VK2COT
Honored Contributor

Re: Read text file from a specified string to the end of the file.

Hello again,

You awk line does not escape the search string.
Try this:

awk '/'"$Last_Alert"'/,EOF {getline;print}' $LogFile >> $LoadFile

VK2COT
VK2COT - Dusan Baljevic
Dennis Handly
Acclaimed Contributor

Re: Read text file from a specified string to the end of the file.

>VK2COT: awk '/'"$Last_Alert"'/,EOF {getline;print}' $LogFile >> $LoadFile

I'm not sure why you have EOF? It seems this is just the empty string. Also your getline causes it to skip every other line.

If you want to start from your $Last_Alert you can use sed:
$ sed -n "/$Last_Alert,$/p" $LogFile >> $LoadFile
Pete Randall
Outstanding Contributor

Re: Read text file from a specified string to the end of the file.

From "Handy One-Liners for Sed" (attached):

# print section of file from regular expression to end of file
sed -n '/regexp/,$p'


Pete

Pete
VK2COT
Honored Contributor

Re: Read text file from a specified string to the end of the file.

Hello,

Dennis is right: getline is not needed as
it prints every second line only.

This works 100%:

awk '/'"$Last_Alert"'/,EOF {print}' $LogFile >> $LoadFile

You need "EOF" or "0" string because
otherwise you only get a matching line.

Cheers,

VK2COT
VK2COT - Dusan Baljevic
bsraj
Occasional Advisor

Re: Read text file from a specified string to the end of the file.

Hi All,

Thanks for all your details. I am still getting the same error when using the awk command, but using the below command i am able to list all the details..
sed -n "/^$Last_Alert/,$ p" $LogFile >> $LoadFile.

But the above command is listing including the specified string.
Please let me is there any way to list the error messages after the specified date and time. (Thu Jan 10 12:56:50 2008)
eg:
Thu Jan 10 12:56:49 2008
err0
Thu Jan 10 12:56:50 2008
err1
Thu Jan 10 12:56:51 2008
err2

if Last_Alert = Thu Jan 10 12:56:50 2008 then, the next time i like to list all the errors only after that.
eg
Thu Jan 10 12:56:51 2008
err2

Thanks,
bsraj.