how to add extra a field in a flat txt file ?

Hi all,

I did not use UNIX for a long time, now i need to make a flat file with extra field, can you help me with the code ?

  1. I create a last line of each log from each system and make it in a flat text file (seperate by a pipe |)
    mv current.log old
    tail -1 sanfrancisco.log > current.log
    tail -1 oakland.log >> current.log

Current.log

2011/04/07 14:49:46 |   61:linux   pid=027546 opened Boards 0, 1, 2, 3 for /home/goniners/abcd/
2011/04/08 08:13:39 |  255:linux   pid=016870 opened Boards 0, 1, 2, 3, 4, 6 for /home/gosteelers/asdas/
  1. How do we create new.log which have a hostname in a 2nd fields ?
2011/04/07 14:49:46 |sanfrancisco|   61:linux   pid=027546 opened Boards 0, 1, 2, 3 for /home/goniners/abcd/
2011/04/08 08:13:39 |oakland|  255:linux   pid=016870 opened Boards 0, 1, 2, 3, 4, 6 for /home/gosteelers/asdas/

Thanks,

sed "s/|/|${HOSTNAME}|/" current_log

try:

awk '{$3=$3 FILENAME"|";sub(/.log/,"",$3);if(FNR<X)print l;X=FNR;l=$0}END{print l}' logfiles >current.log

Thanks all.
I create another host.log
sanfrancisco|
oakland|

Then paste host.log current.log > newlog.txt
I will try your way.
But now how do i get a newlog.txt or current.log like

2011/04/07 14:49:46 |sanfrancisco|goniners|   61:linux   pid=027546 opened Boards 0, 1, 2, 3 for /home/goniners/abcd/
2011/04/08 08:13:39 |oakland|gosteelers|  255:linux   pid=016870 opened Boards 0, 1, 2, 3, 4, 6 for /home/gosteelers/asdas/
$ HOST="myhost"
$ ruby -pne '$_.sub!(/\|/,"|'$HOST'|")' file
2011/04/07 14:49:46 |myhost|   61:linux   pid=027546 opened Boards 0, 1, 2, 3 for /home/goniners/abcd/
2011/04/08 08:13:39 |myhost|  255:linux   pid=016870 opened Boards 0, 1, 2, 3, 4, 6 for /home/gosteelers/asdas/

Thanks, i never use Ruby before :slight_smile: