How to Add Date String to Each Line of a Continuously Written Log File

How to add date string to each line of a continuously written log file

With perl:

command 2>&1 | perl -pe 'print scalar(localtime()), " ";'

With gawk:

command 2>&1 | awk '{ print strftime(), $0; fflush() }'

Replace command with tail -f logfile for your specific example. Or, perhaps you could just redirect the original program's stdout/stderr to the above pipe.

How to add text at the end of each line in unix

There are many ways:

sed: replace $ (end of line) with the given text.

$ sed 's/$/ | COUNTRY/' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

awk: print the line plus the given text.

$ awk '{print $0, "| COUNTRY"}' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

Finally, in pure bash: read line by line and print it together with the given text. Note this is discouraged as explained in Why is using a shell loop to process text considered bad practice?

$ while IFS= read -r line; do echo "$line | COUNTRY"; done < file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

How to increment a date in a Bash script

Use the date command's ability to add days to existing dates.

The following:

DATE=2013-05-25

for i in {0..8}
do
NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day")
echo "$NEXT_DATE"
done

produces:

05-25-2013
05-26-2013
05-27-2013
05-28-2013
05-29-2013
05-30-2013
05-31-2013
06-01-2013
06-02-2013

Note, this works well in your case but other date formats such as yyyymmdd may need to include "UTC" in the date string (e.g., date -ud "20130515 UTC + 1 day").

Write current date/time to a file using shell script

Use date >> //home/user/Desktop/Scripts/Date Logs/datelog.txt.

Like i tried in my system :-

date > /tmp/date.txt.
And file contains Wed Apr 5 09:27:37 IST 2017.

[Edit] There are difference between >>(appending to the file) and >(Create the new file)

Edit:- As suggested by chepner, you can directly redirect the o/p of date command to file using date >> /tmp/date.txt.

Continually read the lines being appended to a log file

I don't know if you're going in the right direction but if I've understood correctly you'll find this useful: java-io-implementation-of-unix-linux-tail-f



Related Topics



Leave a reply



Submit