LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-06-2012, 11:31 AM   #1
nelovicov
LQ Newbie
 
Registered: Jan 2012
Location: in Angola, City Luanda
Posts: 8

Rep: Reputation: Disabled
Smile Insert a char and date into file in the same line everyday


I want to do a script that count an error from files and put the result into text file. I want to put this in a cron to run everyday. all the result will be in the same text file. The dificulty is to put also de date in the same line of each result. Bellow there is the command that I am using for that:

cat /logs/session_event_log/ready/session_event_log.* | grep -c "#113" >> Error113.txt

Could someone help me, please? if something I've wrote bad, please forgive me, 'cause my Inglish is so so .
 
Old 01-06-2012, 12:05 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Simply place the date command redirected into the same file prior to running your original command, eg.
Code:
date >> Error113.txt
 
Old 01-06-2012, 02:31 PM   #3
Samotnik
Member
 
Registered: Jun 2006
Location: Belarus
Distribution: Debian GNU/Linux testing/unstable
Posts: 471

Rep: Reputation: 40
Something like echo "$(date) - $(cat /logs/session_event_log/ready/session_event_log.* | grep -c '#113')" > error113.txt ?

And use > instead of >> or you will end with a new file with only line every day.

Last edited by Samotnik; 01-06-2012 at 02:34 PM.
 
Old 01-12-2012, 02:24 AM   #4
nelovicov
LQ Newbie
 
Registered: Jan 2012
Location: in Angola, City Luanda
Posts: 8

Original Poster
Rep: Reputation: Disabled
Hi

I just think about that, but I want to put the date and the result of error 113 in the same line. And I'm using >> to use the same file.
 
Old 01-12-2012, 09:30 AM   #5
gdejonge
Member
 
Registered: Aug 2010
Location: Netherlands
Distribution: Kubuntu, Debian, Suse, Slackware
Posts: 317

Rep: Reputation: 73
You could use awk to add a timestamp.

Code:
cat /logs/session_event_log/ready/session_event_log.* | grep -c "#113" | awk '{ strftime() " " $0 }' >> Error113.txt
 
Old 01-12-2012, 10:13 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
Quote:
Originally Posted by nelovicov View Post
I just think about that, but I want to put the date and the result of error 113 in the same line. And I'm using >> to use the same file.
You won't get error 113 lines output with grep -c, only the matching lines number
 
Old 01-12-2012, 10:35 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Capture the output into an array variable first, then you can redirect it afterwards along with date. (I'm assuming that the grep output consists of filename+no. of errors, and you want to print the date next to every name.)

Also, useless use of cat.

Code:
mapfile -t errors < <( grep -c "#113" /logs/session_event_log/ready/session_event_log.* )

printf "$(date): %s\n" "${errors[@]}" >> Error113.txt
Note that the above uses a couple of recent bash-only features, however. Otherwise, use a while+read loop to grab each line one-at-a-time and print it separately.

Last edited by David the H.; 01-12-2012 at 10:47 AM. Reason: added last part
 
Old 01-16-2012, 10:07 AM   #8
nelovicov
LQ Newbie
 
Registered: Jan 2012
Location: in Angola, City Luanda
Posts: 8

Original Poster
Rep: Reputation: Disabled
Ex.: into text file I want to Have:
4021 16-01-2012 --> 1st day
3024 17-01-2012 --> 2nd day
.... ....
.... ....

result of error count + date
 
Old 01-17-2012, 12:28 PM   #9
nelovicov
LQ Newbie
 
Registered: Jan 2012
Location: in Angola, City Luanda
Posts: 8

Original Poster
Rep: Reputation: Disabled
Smile Hi David

I made some change and it's working. bellow is de code:
#!/bin/sh

DATE=`date +%F" "%R`

ERROR=`cat /logs/session_event_log/ready/session_event_log.* | grep -c "#113"`

printf "$ERROR %s\n" "$DATE" >> Erro113.txt

thanks for your help
 
  


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
Programing Question: Reading file line by line.. then char by char in each line adriumroot Programming 8 05-30-2010 02:55 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 10:14 PM
insert a line or a file to /etc j-me Linux - Newbie 9 07-23-2009 02:47 PM
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 08:14 PM
Insert date and timestamp Into File name petenyce Linux - Newbie 9 10-13-2005 01:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:31 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration