LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-10-2010, 04:43 AM   #1
jpmon
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Rep: Reputation: 0
I can't understand return value of gawk.


I write a little script by bash.
-------------------------------------------------------------------------
#!/bin/bash
a=`gawk '/Iterations /{print $0}' hoge.log | gawk 'BEGIN{FS=" "}{print ($4)}'`
a=`expr 3 '*' $a`
-------------------------------------------------------------------------
hoge.log is below text.
-- Iterations = 50000

Above srcipt can run in cygwin, Display "150000"
but can't run in Linux, Display "expr: non-numeric argument"
What is different for Linux and cygwin?
I would like tell me how is wrong.
 
Old 03-10-2010, 04:53 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It works as you expect for me on Linux (Slackware 13.0).

What is $a after a=`<gawk pipe>` and before a=`expr command>`?
 
Old 03-10-2010, 08:04 PM   #3
jpmon
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
I would like to read a lot of log files like "hoge*.log",
and extract data to calc for one purpose.

I can't use bc in my system.
Would you tell me alternative way?
 
Old 03-10-2010, 08:38 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by jpmon View Post
I would like to read a lot of log files like "hoge*.log",
and extract data to calc for one purpose.

I can't use bc in my system.
Would you tell me alternative way?
An alternative to bc is expr. If you answered my question it might help us find out why expr is not working for you -- and then we might be able to make it work.

What is the output if you modify the script to
Code:
#!/bin/bash
a=`gawk '/Iterations /{print $0}' hoge.log | gawk 'BEGIN{FS=" "}{print ($4)}'`
echo "a is '$a'"
a=`expr 3 '*' $a`
 
Old 03-10-2010, 10:28 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
And this is hardly a "linux kernel" question . . .
 
Old 03-11-2010, 12:30 AM   #6
jpmon
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
I ran above code.
One log write by vi and Other dump by other executable program.

write by vi
Display "a is '50000'".

dump by other executable program
Display "' is '50000".
I think last character "'" comes around at head of the line.
Dump use only redirect.
What is different?
 
Old 03-11-2010, 12:52 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by jpmon View Post
I ran above code.
One log write by vi and Other dump by other executable program.

write by vi
Display "a is '50000'".

dump by other executable program
Display "' is '50000".
I think last character "'" comes around at head of the line.
Dump use only redirect.
What is different?
What does "dump by other executable program" mean? Did you copy-and-paste the output from running the script?
 
Old 03-11-2010, 01:56 AM   #8
jpmon
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Other executable program is benchmark program.
I making this script for assembling result data from benchmark program's dump.
 
Old 03-11-2010, 03:15 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by jpmon View Post
Other executable program is benchmark program.
I making this script for assembling result data from benchmark program's dump.
Can we keep it simple to start with and just run the script from the command line and copy-and-paste the output to ensure it is exactly what the script wrote? Once that's known to be working OK we can move onto running it in other ways.
 
Old 03-11-2010, 08:24 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
moved to Programming
 
Old 03-11-2010, 09:05 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by jpmon View Post
Above srcipt can run in cygwin, Display "150000"
but can't run in Linux, Display "expr: non-numeric argument"
What is different for Linux and cygwin?
Most likely you're trying to read in Linux a log file created in Windows. Different line terminators between the two systems cause troubles to the shell. To avoid the problem, in Linux you can try
Code:
dos2unix hoge.log
before running the script.

A little hint: you can summarize your statements with a single awk line, e.g. with something like:
Code:
a=`gawk '/Iterations /{print $4*3}' hoge.log`
 
Old 03-11-2010, 07:47 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jpmon View Post
I write a little script by bash.
-------------------------------------------------------------------------
#!/bin/bash
a=`gawk '/Iterations /{print $0}' hoge.log | gawk 'BEGIN{FS=" "}{print ($4)}'`
a=`expr 3 '*' $a`
-------------------------------------------------------------------------
hoge.log is below text.
-- Iterations = 50000

Above srcipt can run in cygwin, Display "150000"
but can't run in Linux, Display "expr: non-numeric argument"
What is different for Linux and cygwin?
I would like tell me how is wrong.
why are you doing an extra pipe to gawk? you can do it with just one gawk statement

Code:
gawk '/Iterations/{  print $4 * 3}' hoge.log
to put the result to shell variable

Code:
a=$(gawk '/Iterations/{  print $4 * 3}' hoge.log)
 
Old 03-11-2010, 07:48 PM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by catkin View Post
An alternative to bc is expr. If you answered my question it might help us find out why expr is not working for you -- and then we might be able to make it work.

What is the output if you modify the script to
Code:
#!/bin/bash
a=`gawk '/Iterations /{print $0}' hoge.log | gawk 'BEGIN{FS=" "}{print ($4)}'`
echo "a is '$a'"
a=`expr 3 '*' $a`

expr cannot do floats.
 
Old 03-11-2010, 11:27 PM   #14
jpmon
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you for the help.
I closure of problem.

The new-line character in my log file is CF+RF.
Inserting "dos2unix hoge.log" in my script, it run well!!!
 
  


Reply



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
dpkg return error :post installation script return an error code (1) grimfold Debian 2 09-10-2009 01:55 PM
gawk help needed. Speedy2k Linux - Newbie 2 11-07-2008 10:10 AM
gawk help.... visitnag Linux - Newbie 1 04-12-2008 11:55 AM
gawk question luxpops Programming 1 09-12-2004 04:46 AM
FS=? in gawk realos Programming 2 05-28-2003 07:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 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
Open Source Consulting | Domain Registration