1827595 Members
2847 Online
109966 Solutions
New Discussion

Echo command usage

 
SOLVED
Go to solution
Karthik S S
Honored Contributor

Echo command usage

Hi, I have a file hpux25.tmp which contains the following entries,

cat hpux25.tmp
----------------------------------------
Time: Fri May 30 23:13:53 2003 ID: hpux25_1054316633 FULL (0)
Time: Sat May 17 02:32:53 2003 ID: hpux25_1053118973 FULL (0)
Time: Tue Jun 10 20:24:58 2003 ID: hpux25_1055256898 FULL (0)
Time: Sun Jun 08 20:01:13 2003 ID: hpux25_1055082673 FULL (0)
Time: Fri Jun 06 20:26:37 2003 ID: hpux25_1054911397 FULL (0)
Time: Tue Jun 03 20:24:24 2003 ID: hpux25_1054652064 FULL (0)
Time: Mon Jun 02 16:25:39 2003 ID: hpux25_1054551339 FULL (0)
Time: Fri May 30 20:53:04 2003 ID: hpux25_1054308184 FULL (0)
Time: Thu May 29 23:35:54 2003 ID: hpux25_1054231554 FULL (0)
Time: Thu May 29 22:18:04 2003 ID: hpux25_1054226884 FULL (0)
Time: Thu May 29 21:27:02 2003 ID: hpux25_1054223822 FULL (0)
Time: Tue May 27 20:23:16 2003 ID: hpux25_1054047196 FULL (0)
Time: Sun May 25 20:00:06 2003 ID: hpux25_1053873006 FULL (0)
Time: Fri May 23 20:32:16 2003 ID: hpux25_1053702136 FULL (0)
Time: Tue May 20 20:30:12 2003 ID: hpux25_1053442812 FULL (0)
Time: Sun May 18 19:58:26 2003 ID: hpux25_1053268106 FULL (0)
----------------------------------------

And I have a written asmall script like this,
cat script1.sh
----------------------------------------
Jan=1
Feb=2
Mar=3
Apr=4
May=5
Jun=6
Jul=7
Aug=8
Sep=9
Oct=10
Nov=11
Dec=12

for i in `cat hpux25.tmp |cut -f3 -d " " | sed s/^/$/g `
do
echo "$i"
done
----------------------------------------

But when I do a echo $i inside the script it echoes merely for ex: $May instead of showing its value which is set to 5.

What could be the problem??

Thanks
Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
14 REPLIES 14
RolandH
Honored Contributor

Re: Echo command usage

You have a littel type in your for statement.

Do

for i in `cat hpux25.tmp |cut -f3 -d " " | sed s/^$//g `

instead of

for i in `cat hpux25.tmp |cut -f3 -d " " | sed s/^/$/g `

then it works.


Regards
Roland
Sometimes you lose and sometimes the others win
Karthik S S
Honored Contributor

Re: Echo command usage

No, Still it is not working by doing

for i in `cat hpux25.tmp |cut -f3 -d " " | sed s/^$//g

sed only removes all the blank lines .. and doesn't make any sense here.

Thanks
Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Jean-Louis Phelix
Honored Contributor
Solution

Re: Echo command usage

Hi,

This is normal, because each line is evaluated before execution :

so you should rather use instead of

echo "$i" evaluated to echo "May" BEFORE execution

eval echo "\$$i", first evaluated to :

echo "$May" then evaluated to 5 ...

Regards.
It works for me (© Bill McNAMARA ...)
Karthik S S
Honored Contributor

Re: Echo command usage

Hi Jean,

The following worked perfectly,

for i in `cat cbin-nmvob1.tmp |cut -f3 -d " " | sed s/^/$/g `
do
eval echo "$i"
done


Thanks a lot,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Ollie R
Respected Contributor

Re: Echo command usage

Hi Karthik,

I agree with Jean-Louis - you must use eval.

Try the following:

for i in `cat hpux25.tmp |cut -f3 -d " "`
do
j='$'$i
eval "k=$j"
echo "$k"
done


Ollie.
To err is human but to not award points is unforgivable
RolandH
Honored Contributor

Re: Echo command usage

Sorry I read question to fast and did not recognize that you want the number instead of the month only.

I have solved it like that way:
Jan=1
Feb=2
Mar=3
Apr=4
May=5
Jun=6
Jul=7
Aug=8
Sep=9
Oct=10
Nov=11
Dec=12

for i in `cat hpux25.tmp |cut -f3 -d " " | sed s/^/$/g `
do
SIGN='$'
eval echo ${SIGN}${i}
done

Regards
Roland
Sometimes you lose and sometimes the others win
John Meissner
Esteemed Contributor

Re: Echo command usage

a possibly easier way to change the date is to change the way the script or system reports the date.

using %m as an option with date

date "+%m/%d/%y"

or by

date -u mmddccyy
All paths lead to destiny
Karthik S S
Honored Contributor

Re: Echo command usage

Hi,

When I pass two fields to the cut command I get the o/p like this,
-----------------------------------------
for i in `cat cbin-nmvob1.tmp |cut -f3,4 -d " " | sed s/^/$/g `
do
eval echo "$i"
done
-----------------------------------------

5
30
5
17
6
10
6
08
6
06
--- part o/p is truncated ---

But I am expecting the o/p like this,
5 30
5 17
6 10
6 08
6 06

How can I achieve this??

Thanks,
Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Jean-Louis Phelix
Honored Contributor

Re: Echo command usage

Hi,

This one should work (but I didn't test it ...)

for i in `cat cbin-nmvob1.tmp |cut -f3,4 -d " " | sed s/^/$/g | tr ' ' '_'`
do
M=$(echo $i | cut -f1 -d'_')
D=$(echo $i | cut -f2 -d'_')
echo x"$(eval echo "$M") $D
done

Regards.
It works for me (© Bill McNAMARA ...)
Jean-Louis Phelix
Honored Contributor

Re: Echo command usage

Hi,

This one should work (but I didn't test it ...)

for i in `cat cbin-nmvob1.tmp |cut -f3,4 -d " " | sed s/^/$/g | tr ' ' '_'`
do
M=$(echo $i | cut -f1 -d'_')
D=$(echo $i | cut -f2 -d'_')
echo "$(eval echo "$M") $D
done

Regards.
It works for me (© Bill McNAMARA ...)
Jean-Louis Phelix
Honored Contributor

Re: Echo command usage

Hi,

This one should work (but I didn't test it ...)

for i in `cat cbin-nmvob1.tmp |cut -f3,4 -d " " | sed s/^/$/g | tr ' ' '_'`
do
M=$(echo $i | cut -f1 -d'_')
D=$(echo $i | cut -f2 -d'_')
echo $(eval echo "$M") $D
done

Regards.
It works for me (© Bill McNAMARA ...)
Ollie R
Respected Contributor

Re: Echo command usage

Hi Karthik,

The best way is to read each line individually using a construct similar to:

cat | while read LINE
do

done

Hope this helps,


Ollie.
To err is human but to not award points is unforgivable
Ollie R
Respected Contributor

Re: Echo command usage

Hi Karthik,

The best way is to read each line individually using a construct similar to:

cat | while read LINE
do

done

Hope this helps,


Ollie.
To err is human but to not award points is unforgivable
Ian Dennison_1
Honored Contributor

Re: Echo command usage

You need a loop as follows,...

echo /tmp/file |sed 'etc etc etc' >/tmp/inputfile

while read a b
do
# a = first argument
# b = second argument
eval echo "$a $b"

done
Share and Enjoy! Ian
Building a dumber user