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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-23-2011, 06:28 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11
Rep:
|
echo concat values
I have been playing with this for hours. I have two values I set min and max. When I use echo on them seperately they print the values fine. When I concat the values in echo the first value does not print. When I set the values outside of awk using the terminal interactivly it prints fine. When I tried
Code:
echo -n $max + $min ]
nothing prints.
CALL TO SCRIPT
Code:
$ ./genbnds.sh polprmest.R 100 20
57.92
24.74
+ 24.74
SCRIPT
Code:
...
#FIND MIN VALUE
min=`awk 'BEGIN{FS=","}; min=="" || $3 < min {min=$3} END {print min}' nonhead`
#FIND MAX VALUE
max=`awk 'BEGIN{FS=","}; max=="" || $3 > max {max=$3} END {print max}' nonhead`
echo $max
echo $min
echo $max + $min
...
|
|
|
|
05-23-2011, 06:37 AM
|
#2
|
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,406
|
It works for me. Verify that you are using bash.
Code:
#!/bin/bash
max=57.92
min=24.74
echo -n $max + $min ]
|
|
|
|
05-23-2011, 06:51 AM
|
#3
|
|
LQ Newbie
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11
Original Poster
Rep:
|
I am using bash and it does work fine if I set the values specifically like you did. It only happens when they are set using awk.
Here is the file I am using awk on. Not sure if that helps
Code:
1,"2007-11-30",24.74
1,"2008-03-31",42.62
1,"2008-04-30",41.27
1,"2008-05-31",42.62
1,"2008-06-30",41.27
1,"2008-07-31",42.62
1,"2008-08-31",42.62
1,"2008-09-30",41.27
1,"2008-10-31",42.62
1,"2008-11-30",41.24
1,"2008-12-31",42.62
1,"2009-01-31",42.62
1,"2009-02-28",38.49
1,"2009-03-31",42.62
1,"2009-04-30",41.27
1,"2009-05-31",42.62
1,"2009-06-30",41.82
1,"2009-07-31",51.55
1,"2009-08-31",51.55
1,"2009-09-30",49.91
1,"2009-10-31",51.55
1,"2009-11-30",53.60
1,"2009-12-31",57.92
1,"2010-01-31",57.92
1,"2010-02-28",52.31
1,"2010-03-31",57.92
1,"2010-04-30",56.07
1,"2010-05-31",57.92
1,"2010-06-30",56.07
1,"2010-07-31",57.92
1,"2010-08-31",57.92
1,"2010-09-30",56.07
1,"2010-10-31",57.92
1,"2010-11-30",50.04
1,"2010-12-31",47.56
1,"2011-01-31",47.56
1,"2011-02-28",42.96
1,"2011-03-31",47.56
|
|
|
|
05-23-2011, 07:07 AM
|
#4
|
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,406
|
Quote:
Originally Posted by abercrombieande
Here is the file I am using awk on. Not sure if that helps
|
Of course it helps!
Anyhow, the following (still) works:
Code:
#!/bin/bash
#FIND MIN VALUE
min=`awk 'BEGIN{FS=","}; min=="" || $3 < min {min=$3} END {print min}' nonhead`
#FIND MAX VALUE
max=`awk 'BEGIN{FS=","}; max=="" || $3 > max {max=$3} END {print max}' nonhead`
echo -n $max + $min ]
echo ""
I copied/pasted the data you provided into a file called "nonhead".
|
|
|
|
05-23-2011, 07:20 AM
|
#5
|
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,024
Rep: 
|
If the text files has windows end of lines - ascii 13, it will be included in the end of the variable.
Edit, like this:
echo -e '57.92\r + 24.74'
Last edited by Guttorm; 05-23-2011 at 07:23 AM.
|
|
|
|
05-23-2011, 07:52 AM
|
#6
|
|
LQ Newbie
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by dwhitney67
Of course it helps!
Anyhow, the following (still) works:
Code:
#!/bin/bash
#FIND MIN VALUE
min=`awk 'BEGIN{FS=","}; min=="" || $3 < min {min=$3} END {print min}' nonhead`
#FIND MAX VALUE
max=`awk 'BEGIN{FS=","}; max=="" || $3 > max {max=$3} END {print max}' nonhead`
echo -n $max + $min ]
echo ""
I copied/pasted the data you provided into a file called "nonhead".
|
Thanks for the help with this whats with the ] in
Code:
echo -n $max + $min ]
Maybe its the version of bash I am using. Currently at 4.1.5(1)-release (i486-pc-linux-g
|
|
|
|
05-23-2011, 07:59 AM
|
#7
|
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,024
Rep: 
|
Did you try removing the \r characters?
Code:
min=`awk 'BEGIN{FS=","}; min=="" || $3 < min {min=$3} END {print min}' nonhead |tr -d '\r'`
max=`awk 'BEGIN{FS=","}; max=="" || $3 > max {max=$3} END {print max}' nonhead |tr -d '\r'`
There's probably an awk way to do this as well.
|
|
|
|
05-23-2011, 08:04 AM
|
#8
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,386
|
Well I have 2 questions:
1. Are you able to echo the variables on there own, eg. echo $min?
2. Why not just do it all in awk?
|
|
|
|
05-23-2011, 08:05 AM
|
#9
|
|
LQ Newbie
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by Guttorm
Did you try removing the \r characters?
Code:
min=`awk 'BEGIN{FS=","}; min=="" || $3 < min {min=$3} END {print min}' nonhead |tr -d '\r'`
max=`awk 'BEGIN{FS=","}; max=="" || $3 > max {max=$3} END {print max}' nonhead |tr -d '\r'`
There's probably an awk way to do this as well.
|
God I hate windows
Code:
max=`echo $max | tr -d '\r'`
min=`echo $min | tr -d '\r'`
echo $max
echo $min
Thanks Everyone for the help
|
|
|
|
05-23-2011, 08:07 AM
|
#10
|
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,406
|
Quote:
Originally Posted by abercrombieande
Thanks for the help with this whats with the ] in
Code:
echo -n $max + $min ]
|
You had the bracket in your OP; that's why I included it in the sample script.
|
|
|
|
05-23-2011, 08:10 AM
|
#11
|
|
LQ Newbie
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by dwhitney67
You had the bracket in your OP; that's why I included it in the sample script.
|
Today isnt my day..
|
|
|
|
05-23-2011, 09:18 AM
|
#12
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,386
|
Just to add onto Guttorm's previous statement, you can indeed use awk to remove the \r if you are constantly dealing with Windows files.
Take a look at the RS component of awk, is default is a newline (\n) but it can be set to multiple values, such as:
|
|
|
|
05-23-2011, 09:50 AM
|
#13
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Quote:
Originally Posted by grail
|
That will turn multiple newlines into one. This is what I would do:
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:10 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|