LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-02-2003, 09:56 PM   #1
Hardw1re
LQ Newbie
 
Registered: Nov 2003
Posts: 20

Rep: Reputation: 0
reading a text file and outputting to another.


Can someone help with the basics of creating a command that will read a .log file and strip the output to another file that just conatins certain aspects of that original eg:

Scriptlog: ........

basically the log contains loads of data, but i just want a file that shows anything begining with Scriptlog:

i know that to ouput to the file i can do
date $var >> scriptlog.log

or something similar. im extremely new to programing and have a small knowledge of C ( but only windows console applications)
 
Old 11-02-2003, 10:10 PM   #2
Hardw1re
LQ Newbie
 
Registered: Nov 2003
Posts: 20

Original Poster
Rep: Reputation: 0
oh and btw i have made something similar in C running as a console application. but how do i do this as a shell script, and i read another thread about make, which updates when the file is edited by something else (eg in my case the server adding to the log.)
 
Old 11-02-2003, 10:11 PM   #3
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
Well how abt this
Code:
cat scriptlog.log | awk -F ":" '{if($1=="Scriptlog"){print $0}}' > strip.log
I am using awk to do this . Let me explain

1. I am using : as the field seperator . So, the fields will be stored in $1 $2 so on
2. Then I check if field 1 is equal to "Scriptlog" or not
3. If it is Scriptlog then I just print $0 [ $0 contains the entire line ]
4.This thing I write to another file strip.log
 
Old 11-02-2003, 10:24 PM   #4
Hardw1re
LQ Newbie
 
Registered: Nov 2003
Posts: 20

Original Poster
Rep: Reputation: 0
wat exactly does the cat bit do? and does this scan the entire log? and can i make it do this when the log is modified?
 
Old 11-02-2003, 10:29 PM   #5
Hardw1re
LQ Newbie
 
Registered: Nov 2003
Posts: 20

Original Poster
Rep: Reputation: 0
Just logged in to my server with ssh, and done help commands, so eg:
cat -A /home/server.log | (then your awk code etc)
 
Old 11-02-2003, 10:31 PM   #6
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
It scans the file line by line and so by using a broken pipe | we can put the code into operation

i.e, cat some_file | blahblahblah

where blahblahblah is what you want to do with the file
<edit>
Did my code give u successful results . Was that what you wanted ??
</edit>

Last edited by SaTaN; 11-02-2003 at 10:34 PM.
 
Old 11-02-2003, 10:33 PM   #7
Hardw1re
LQ Newbie
 
Registered: Nov 2003
Posts: 20

Original Poster
Rep: Reputation: 0
Ok, im going to specify further. this is a game server's log, and obviously every time a play joins or it changes map its going to write to the log, is there a way of making the code continue from where it left off and make it so the script is always reading the log file to add the lines?
 
Old 11-02-2003, 10:36 PM   #8
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
Use crontab I suppose that would help
or
Put the code you want to run in a while(1) loop and then run this code

Last edited by SaTaN; 11-02-2003 at 10:38 PM.
 
Old 11-02-2003, 10:38 PM   #9
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Or easier:
Code:
awk '/^Scriptlog:/ {print}' scriptlog.log > strip.log
Or with sed:
Code:
sed -n '/^Scriptlog:/p' scriptlog.log > strip.log
Or with perl:
Code:
perl -ne 'print if /^Scriptlog:/' scriptlog.log > strip.log
Or with grep:
Code:
grep '^Scriptlog:' scriptlog.log > strip.log
Pick any tool you want.
 
Old 11-02-2003, 10:39 PM   #10
Hardw1re
LQ Newbie
 
Registered: Nov 2003
Posts: 20

Original Poster
Rep: Reputation: 0
O_o oh ek you know how to confuse a n00b
 
Old 11-02-2003, 10:44 PM   #11
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
If it is using crontab then do it this way
type "crontab -e" on the konsole

Then a file will be opened and then in that file
Code:
MAILTO=""
* * * * * cat scriptlog.log | awk -F ":" '{if($1=="Scriptlog"){print $0}}' >> strip.log
* * * * * fields are as follows minute ,hour,day of month ,month ,day of week

* means it'll be run always . So for every minute your file will be updated
Check out man 5 crontab for more details

Last edited by SaTaN; 11-02-2003 at 10:45 PM.
 
Old 11-02-2003, 10:45 PM   #12
Hardw1re
LQ Newbie
 
Registered: Nov 2003
Posts: 20

Original Poster
Rep: Reputation: 0
Well i used:
awk '/^MMCv110:/ {print}' RGserver.log > strip.log
and that worked this is wot it outputted:
MMCv110: ##########################################
MMCv110: # ModMenuChecker v1.10 by TA`Tremor (PD) #
MMCv110: ##########################################
MMCv110: [MMC] -rG.Hardw1re- :
MMCv110: [MMC] -rG.Hardw1re- : AdvancedInstaGib.AIG_ModMenuItem
MMCv110: [MMC] -rG.Hardw1re- : ChaosUT.ChaosUTModMenuItem
MMCv110: [MMC] -rG.Hardw1re- : ChatLog.ChatLogModMenuItem
MMCv110: [MMC] -rG.Hardw1re- : IRC_Reporter.IRC_Reporter
MMCv110: [MMC] -rG.Hardw1re- : JoltDemoEnhancer2.DEMenuItem
MMCv110: [MMC] -rG.Hardw1re- : MultiMesh.MultiMeshMenu
MMCv110: [MMC] -rG.Hardw1re- : udemo.UDmodItem
MMCv110: [MMC] -rG.Hardw1re- : Weaponer.WeaponerModMenuItem

So if i do a .sh file with this in a while(1){ } loop and make the auto restart script for the server that i use to boot the server run the .sh file this will make it write to the file all the time right??
 
Old 11-02-2003, 10:46 PM   #13
Hardw1re
LQ Newbie
 
Registered: Nov 2003
Posts: 20

Original Poster
Rep: Reputation: 0
Thank You so much Satan, i appreciate the help you have given me.

Hardw1re.
 
Old 11-02-2003, 10:50 PM   #14
Hardw1re
LQ Newbie
 
Registered: Nov 2003
Posts: 20

Original Poster
Rep: Reputation: 0
hopefully my final post, will this .sh file work:

#!/bin/sh
while(1){ awk '/^MMCv110:/ {print}' /home/utpub2/ut-server/System/RGserver.log > /home/utpub2/strip.log }
while(1){ awk '/^UTPure:/ {print}' /home/utpub2/ut-server/System/RGserver.log > /home/utpub2/strip.log }
done
 
Old 11-02-2003, 10:52 PM   #15
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
For persistent following of the log, you can replace the cat (in my examples there were no cat, but it can be put in the beginning of the line with scriptlog.log word removed from the middle.) with a 'tail -f' command. for example:
Code:
 tail -f scriptlog.log | grep '^Scriptlog:' > strip.log
Drawback: only new data in being written to the log.

Also if you want to do the check only once and a while, you could use the
logtail tool. Syntaxically just replacing the cat or tail -f with logtail.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading text from a file mrobertson Programming 16 06-28-2005 12:39 PM
outputting data from a directory to a text file? minm Linux - Newbie 2 12-19-2004 06:46 PM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM
Bash script - reading from text file twantrd Programming 4 11-24-2004 12:38 AM
C++ reading from text file query lrt2003 Programming 5 05-15-2004 04:25 AM


All times are GMT -5. The time now is 08:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration