LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-20-2017, 04:45 AM   #1
rashmi88
LQ Newbie
 
Registered: Jun 2017
Posts: 6

Rep: Reputation: Disabled
Redirect error message to logfile


please help me in redirecting the error messages of this code to a log file.

for f in *.log; do
awk 'NF && NR>1 && $0!~/total:/
{
things_cost=$2*$3;
overalltotal=(overalltotal!="")? overalltotal"+"things_cost : things_cost;
if(things_cost!=$4)
{
things_er[$1]=things_cost" instead of "$4
}
err_t+=$4; t+=things_cost;
}
$0~/total/ && err_t
{
print "Error in calculations:";
for(i in things_er)
{
print "Things total for "i" is wrong: it should be "things_er[i]
}
print "Overalltotal is wrong: It should be "t; next
}1' "$f" 2> error.log
done
 
Old 06-20-2017, 05:35 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
you should use:
Code:
print "some text" > "/dev/stderr"
in your awk script to print message to stderr. and 2>error.log will collect it and save in that file.
 
1 members found this post helpful.
Old 06-20-2017, 06:00 AM   #3
rashmi88
LQ Newbie
 
Registered: Jun 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
Nothing is writing to file error.log is blank. I used the code in the below format. Please let me know where i am going wrong.

for f in *.log; do
awk 'NF && NR>1 && $0!~/total:/
{
things_cost=$2*$3;
overalltotal=(overalltotal!="")? overalltotal"+"things_cost : things_cost;
if(things_cost!=$4)
{
things_er[$1]=things_cost" instead of "$4
}
err_t+=$4; t+=things_cost;
}
$0~/total/ && err_t
{
print "Error in calculations:";
for(i in things_er)
{
print "Things total for "i" is wrong: it should be "things_er[i]
}
print "Overalltotal is wrong: It should be "t; next
}1' "$f" > "/dev/stderr" 2>error.log
done
 
Old 06-20-2017, 06:14 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
Code:
print "Error in calculations:" > "/dev/stderr"
and similar....
 
Old 06-20-2017, 09:00 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Like Pan said

I do not know awk but you still need a command to get the information to a path then a file (name).

Code:
1' "$f" > "/dev/stderr" 2>error.log
that is just a set of directives without a command to start them off properly. It doesn't do anything because their is nothing in front of it to "push" it to /dev/stderr and a log file



gawk/manual/stderr

https://www.gnu.org/software/gawk/ma...pecial-FD.html

Last edited by BW-userx; 06-20-2017 at 09:04 AM.
 
  


Reply

Tags
bash, shell script


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
[SOLVED] How to redirect the error message? Mike_Brown Linux - Newbie 4 05-25-2015 05:50 AM
How to redirect the output to a logfile? okhalid Linux - General 5 08-25-2008 08:59 AM
Shell scripting: Print output to logfile, error to logfile & screen stefanlasiewski Programming 18 05-22-2008 12:47 PM
E-mail client "Nail" doesn't work... And no error message/logfile that I know of! Nathan-H2-Dog Linux - Software 1 01-05-2006 09:36 AM
where is the boot-message logfile? minike Slackware 5 05-23-2005 04:47 PM

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

All times are GMT -5. The time now is 12:20 PM.

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
Open Source Consulting | Domain Registration