LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 05-23-2011, 06:28 AM   #1
abercrombieande
LQ Newbie
 
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11

Rep: Reputation: 0
Question 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
...
 
Old 05-23-2011, 06:37 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
It works for me. Verify that you are using bash.

Code:
#!/bin/bash

max=57.92
min=24.74

echo -n $max + $min ]
 
Old 05-23-2011, 06:51 AM   #3
abercrombieande
LQ Newbie
 
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 05-23-2011, 07:07 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by abercrombieande View Post
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".
 
Old 05-23-2011, 07:20 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,115

Rep: Reputation: 218Reputation: 218Reputation: 218
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.
 
Old 05-23-2011, 07:52 AM   #6
abercrombieande
LQ Newbie
 
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dwhitney67 View Post
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
 
Old 05-23-2011, 07:59 AM   #7
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,115

Rep: Reputation: 218Reputation: 218Reputation: 218
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.
 
Old 05-23-2011, 08:04 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,426

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
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?
 
Old 05-23-2011, 08:05 AM   #9
abercrombieande
LQ Newbie
 
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Guttorm View Post
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
 
Old 05-23-2011, 08:07 AM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by abercrombieande View Post
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.
 
Old 05-23-2011, 08:10 AM   #11
abercrombieande
LQ Newbie
 
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dwhitney67 View Post
You had the bracket in your OP; that's why I included it in the sample script.
Today isnt my day..
 
Old 05-23-2011, 09:18 AM   #12
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,426

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
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]+"
 
Old 05-23-2011, 09:50 AM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by grail View Post
Code:
RS="[\n\r]+"
That will turn multiple newlines into one. This is what I would do:

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


Reply

Tags
awk, bash, echo


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
grep | xargs -I echo $(foo; bar; echo $(fee; fi; fo; fum)) == questionable output.. GrapefruiTgirl Programming 11 12-07-2010 07:02 PM
[SOLVED] video (avi) concat johnh10000 Linux - Software 2 03-23-2010 03:17 AM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 08:25 AM
A question about C concat || dogbird Programming 22 08-16-2006 02:15 AM
Concat string (not ended ...) os2 Programming 2 03-25-2005 03:23 PM


All times are GMT -5. The time now is 07:04 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration