LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   echo concat values (http://www.linuxquestions.org/questions/programming-9/echo-concat-values-882231/)

abercrombieande 05-23-2011 07:28 AM

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
...


dwhitney67 05-23-2011 07:37 AM

It works for me. Verify that you are using bash.

Code:

#!/bin/bash

max=57.92
min=24.74

echo -n $max + $min ]


abercrombieande 05-23-2011 07:51 AM

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


dwhitney67 05-23-2011 08:07 AM

Quote:

Originally Posted by abercrombieande (Post 4364440)
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".

Guttorm 05-23-2011 08:20 AM

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'

abercrombieande 05-23-2011 08:52 AM

Quote:

Originally Posted by dwhitney67 (Post 4364458)
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

Guttorm 05-23-2011 08:59 AM

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.

grail 05-23-2011 09:04 AM

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?

abercrombieande 05-23-2011 09:05 AM

Quote:

Originally Posted by Guttorm (Post 4364516)
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

dwhitney67 05-23-2011 09:07 AM

Quote:

Originally Posted by abercrombieande (Post 4364506)
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.

abercrombieande 05-23-2011 09:10 AM

Quote:

Originally Posted by dwhitney67 (Post 4364522)
You had the bracket in your OP; that's why I included it in the sample script.

Today isnt my day..

grail 05-23-2011 10:18 AM

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:
Code:

RS="[\n\r]+"

MTK358 05-23-2011 10:50 AM

Quote:

Originally Posted by grail (Post 4364593)
Code:

RS="[\n\r]+"

That will turn multiple newlines into one. This is what I would do:

Code:

RS="\n\r?|\r\n?"


All times are GMT -5. The time now is 05:10 AM.